Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

PHP How to get value from array key entered by user?, PHP, complex nested array

views
     
TSjason991
post Aug 18 2016, 08:21 PM, updated 8y ago

is all about the money, money, money
******
Senior Member
1,189 posts

Joined: Sep 2007
From: Taman Desa


Hi guys i just started learning php and stuck on this question, i cant seem to be able to pull data from the nested array.
i have tried array_column which i cant able to implement it even after i upgrade my php to 5.6, please help me cry.gif
currently im using only notepadd++ and xammp to learn php.

CODE

<?php
$transaction = [
'id' => 1795,
'case_number' => 'TX2015-0123',
'property' => [
'id' => 69134,
'propertyType' => [
'id' => 1,
'name' => 'Residential'
],
'address' => [
'block' => '160',
'street' => 'Paya Lebar Road',
'subpremise' => '#07-05',
'building' => 'Orion Industrial Building',
'country_code' => 'SG'
],
'askingPrice' => '2500000.00',
'photos' => [
[
'url' =>
'https://www.prosage.com/thumbnails/600x400F/1c93ad90-639b-4dlc-988b-b9
8f75364db0.jpg',
'is_default' => 1
],
[
'url' =>
'https://www.prosage.com/thumbnails/600x400F/8de99fb6-9545-4f5f-8dfc-23
5a57a2b8c1.jpg',
'is_default' => 0
],
[
'url' =>
'https://www.prosage.com/thumbnails/600x400F/e595918f-8b22-4250-9b29-72
c1d3f5dc45.jpg',
'is_default' => 0
]
]
],
];

function getValueByPath($array, $keys)
{
  return array_column($array, $keys);


}

print_r(getValueByPath($transaction, 'property.photos.0.url'));
?>


This post has been edited by jason991: Aug 19 2016, 02:12 AM
shoguniphicus
post Aug 19 2016, 09:43 AM

Casual
***
Junior Member
428 posts

Joined: Jul 2005
From: Music Garden



I thought it's just
$transaction['property']['photos'][0]['url'];

But it's better to loop through the array than to access the property directly.
alien3d
post Aug 19 2016, 09:53 AM

Look at all my stars!!
*******
Senior Member
3,740 posts

Joined: Mar 2009
QUOTE(jason991 @ Aug 18 2016, 08:21 PM)
Hi guys i just started learning php and stuck on this question, i cant seem to be able to pull data from the nested array.
i have tried array_column which i cant able to implement it even after i upgrade my php to 5.6, please help me  cry.gif
currently im using only notepadd++ and xammp to learn php.

CODE

<?php
$transaction = [
'id' => 1795,
'case_number' => 'TX2015-0123',
'property' => [
'id' => 69134,
'propertyType' => [
'id' => 1,
'name' => 'Residential'
],
'address' => [
'block' => '160',
'street' => 'Paya Lebar Road',
'subpremise' => '#07-05',
'building' => 'Orion Industrial Building',
'country_code' => 'SG'
],
'askingPrice' => '2500000.00',
'photos' => [
[
'url' =>
'https://www.prosage.com/thumbnails/600x400F/1c93ad90-639b-4dlc-988b-b9
8f75364db0.jpg',
'is_default' => 1
],
[
'url' =>
'https://www.prosage.com/thumbnails/600x400F/8de99fb6-9545-4f5f-8dfc-23
5a57a2b8c1.jpg',
'is_default' => 0
],
[
'url' =>
'https://www.prosage.com/thumbnails/600x400F/e595918f-8b22-4250-9b29-72
c1d3f5dc45.jpg',
'is_default' => 0
]
]
],
];

function getValueByPath($array, $keys)
{
  return array_column($array, $keys);
}

print_r(getValueByPath($transaction, 'property.photos.0.url'));
?>

*
var_dump or print_r should be enough.
TSjason991
post Aug 19 2016, 10:52 AM

is all about the money, money, money
******
Senior Member
1,189 posts

Joined: Sep 2007
From: Taman Desa


QUOTE(shoguniphicus @ Aug 19 2016, 09:43 AM)
I thought it's just
$transaction['property']['photos'][0]['url'];

But it's better to loop through the array than to access the property directly.
*
yeah have to be dynamic, bro can teach me a better way to loop through the array? haha
TSjason991
post Aug 19 2016, 10:54 AM

is all about the money, money, money
******
Senior Member
1,189 posts

Joined: Sep 2007
From: Taman Desa


QUOTE(alien3d @ Aug 19 2016, 09:53 AM)
var_dump or print_r should be enough.
*
er is not returning anything rather just array(0) { }
alien3d
post Aug 19 2016, 10:56 AM

Look at all my stars!!
*******
Senior Member
3,740 posts

Joined: Mar 2009
QUOTE(jason991 @ Aug 19 2016, 10:54 AM)
er is not returning anything rather just array(0) { }
*
just output here...
TSjason991
post Aug 19 2016, 11:12 AM

is all about the money, money, money
******
Senior Member
1,189 posts

Joined: Sep 2007
From: Taman Desa


QUOTE(alien3d @ Aug 19 2016, 10:56 AM)
just output here...
*
er sorry i dont get what you mean, the output that i get after im using var_dump is just array(0) { }. i think i did wrong some where but most importantly
my getvaluepath function definitely not right as i can't utilize the array_column function, is there another way to solve my case?
alien3d
post Aug 19 2016, 11:24 AM

Look at all my stars!!
*******
Senior Member
3,740 posts

Joined: Mar 2009
QUOTE(jason991 @ Aug 19 2016, 11:12 AM)
er sorry i dont get what you mean, the output that i get after im using var_dump is just array(0) { }. i think i did wrong some where but most importantly
my getvaluepath function definitely not right as i can't utilize the array_column function, is there another way to solve my case?
*
If so may wrong. ** because im not sure how new php 7 work.

Goto php.net find array good example out there. I can overwrite your array but seem not ought to ye
TSjason991
post Aug 19 2016, 03:32 PM

is all about the money, money, money
******
Senior Member
1,189 posts

Joined: Sep 2007
From: Taman Desa


QUOTE(alien3d @ Aug 19 2016, 11:24 AM)
If so may wrong. ** because im not sure how new php 7 work.

Goto php.net find array good example out there. I can overwrite your array but seem not ought to ye
*
well i dont mind if you can overwrite my getvaluebypath function biggrin.gif , im using only 5.6 though hmmm
i guess i will have a look at the php.net den
alien3d
post Aug 19 2016, 04:11 PM

Look at all my stars!!
*******
Senior Member
3,740 posts

Joined: Mar 2009
QUOTE(jason991 @ Aug 19 2016, 11:12 AM)
er sorry i dont get what you mean, the output that i get after im using var_dump is just array(0) { }. i think i did wrong some where but most importantly
my getvaluepath function definitely not right as i can't utilize the array_column function, is there another way to solve my case?
*
when i reply from my phone.. i do i have feeling u don't do what i ask ................. this is from php 5.4
why my output not ! array(0) {} ? instead i got this from var_dump.. from here you don't even need what getvaluepath function..

CODE


array(3) {
 ["id"]=>
 int(1795)
 ["case_number"]=>
 string(11) "TX2015-0123"
 ["property"]=>
 array(5) {
   ["id"]=>
   int(69134)
   ["propertyType"]=>
   array(2) {
     ["id"]=>
     int(1)
     ["name"]=>
     string(11) "Residential"
   }
   ["address"]=>
   array(5) {
     ["block"]=>
     string(3) "160"
     ["street"]=>
     string(15) "Paya Lebar Road"
     ["subpremise"]=>
     string(6) "#07-05"
     ["building"]=>
     string(25) "Orion Industrial Building"
     ["country_code"]=>
     string(2) "SG"
   }
   ["askingPrice"]=>
   string(10) "2500000.00"
   ["photos"]=>
   array(3) {
     [0]=>
     array(2) {
       ["url"]=>
       string(86) "https://www.prosage.com/thumbnails/600x400F/1c93ad90-639b-4dlc-988b-b9
8f75364db0.jpg"
       ["is_default"]=>
       int(1)
     }
     [1]=>
     array(2) {
       ["url"]=>
       string(86) "https://www.prosage.com/thumbnails/600x400F/8de99fb6-9545-4f5f-8dfc-23
5a57a2b8c1.jpg"
       ["is_default"]=>
       int(0)
     }
     [2]=>
     array(2) {
       ["url"]=>
       string(86) "https://www.prosage.com/thumbnails/600x400F/e595918f-8b22-4250-9b29-72
c1d3f5dc45.jpg"
       ["is_default"]=>
       int(0)
     }
   }
 }
}

TSjason991
post Aug 19 2016, 05:03 PM

is all about the money, money, money
******
Senior Member
1,189 posts

Joined: Sep 2007
From: Taman Desa


QUOTE(alien3d @ Aug 19 2016, 04:11 PM)
when i reply from my phone.. i do i have feeling u don't do what i ask ................. this is from php 5.4
why my output not ! array(0) {} ? instead i got this from var_dump..  from here you don't even need what getvaluepath function..

CODE


array(3) {
 ["id"]=>
 int(1795)
 ["case_number"]=>
 string(11) "TX2015-0123"
 ["property"]=>
 array(5) {
   ["id"]=>
   int(69134)
   ["propertyType"]=>
   array(2) {
     ["id"]=>
     int(1)
     ["name"]=>
     string(11) "Residential"
   }
   ["address"]=>
   array(5) {
     ["block"]=>
     string(3) "160"
     ["street"]=>
     string(15) "Paya Lebar Road"
     ["subpremise"]=>
     string(6) "#07-05"
     ["building"]=>
     string(25) "Orion Industrial Building"
     ["country_code"]=>
     string(2) "SG"
   }
   ["askingPrice"]=>
   string(10) "2500000.00"
   ["photos"]=>
   array(3) {
     [0]=>
     array(2) {
       ["url"]=>
       string(86) "https://www.prosage.com/thumbnails/600x400F/1c93ad90-639b-4dlc-988b-b9
8f75364db0.jpg"
       ["is_default"]=>
       int(1)
     }
     [1]=>
     array(2) {
       ["url"]=>
       string(86) "https://www.prosage.com/thumbnails/600x400F/8de99fb6-9545-4f5f-8dfc-23
5a57a2b8c1.jpg"
       ["is_default"]=>
       int(0)
     }
     [2]=>
     array(2) {
       ["url"]=>
       string(86) "https://www.prosage.com/thumbnails/600x400F/e595918f-8b22-4250-9b29-72
c1d3f5dc45.jpg"
       ["is_default"]=>
       int(0)
     }
   }
 }
}

*
the whole point of this question is to get a function called getvaluebypath or watever which will filter out based on the key insert by user, i do have a feeling you dun understand the question

alien3d
post Aug 19 2016, 05:10 PM

Look at all my stars!!
*******
Senior Member
3,740 posts

Joined: Mar 2009
QUOTE(jason991 @ Aug 19 2016, 05:03 PM)
the whole point of this question is to get a function called getvaluebypath or watever which will filter out based on the key insert by user, i do have a feeling you dun understand the question
*
QUOTE
How to get value from array key entered by user?

this is your question.

You question suppose How do i search value inside an array
http://php.net/manual/en/function.array-search.php

or just make it simple.. put in db and search.. don't make thing over complicated.

** rude people nowdays . blush.gif

This post has been edited by alien3d: Aug 19 2016, 05:13 PM
TSjason991
post Aug 19 2016, 05:13 PM

is all about the money, money, money
******
Senior Member
1,189 posts

Joined: Sep 2007
From: Taman Desa


QUOTE(alien3d @ Aug 19 2016, 05:10 PM)
this is your question. 

You question suppose  How do i search value  inside an array
http://php.net/manual/en/function.array-search.php

or just make it simple.. put in db and search.. don't make thing over complicated.
*
wow make thing over complicated? you really posting for the sake of increasing post count eh? read my question again if you dont understand
alien3d
post Aug 19 2016, 05:14 PM

Look at all my stars!!
*******
Senior Member
3,740 posts

Joined: Mar 2009
QUOTE(jason991 @ Aug 19 2016, 05:13 PM)
wow make thing over complicated? you really posting for the sake of increasing post count eh? read my question again if you dont understand
*
Post count ? WTF.. i never care about it.. been doing this for long long time ago since 2001 era php3 starting.
TSjason991
post Aug 19 2016, 05:16 PM

is all about the money, money, money
******
Senior Member
1,189 posts

Joined: Sep 2007
From: Taman Desa


QUOTE(alien3d @ Aug 19 2016, 05:14 PM)
Post count ? WTF..  i never care about it.. been doing this for long long time ago since 2001 era php3 starting.
*
no offence but you can't even answer my question so yea about your coding history....
alien3d
post Aug 19 2016, 05:24 PM

Look at all my stars!!
*******
Senior Member
3,740 posts

Joined: Mar 2009
QUOTE(jason991 @ Aug 19 2016, 05:16 PM)
no offence but you can't even answer my question so yea about your coding history....
*
this is simple .. the most thing is ..

1. Before output

I got 20 record of something.. i need to filter the list according to my data from json / array list.


2. after output should be..

After filter or search it will reduce to 1 record or not

How do i search ?

2.1 Need to loop all the array .. using foreach by e.g
2.2 Need to used strpos to check value same or not ..

Or i kinda by lazy if i can used lala function ? How?

2.X i dono this output so ask web


3. IF i got the same value ?What should i do.. ?

3.1 Are should copy the new array ?
3.2 Are it just output .."Yeah i'm found it "



angch
post Aug 19 2016, 07:23 PM

On my way
****
Junior Member
635 posts

Joined: Jul 2006
https://gist.github.com/angch/87957672a8458...37cbaf49e126a99
CODE
function getValueByPath($array, $keys)
{
   // screw you, php is not javascript, stop traversing structures with "."
   $ks = explode(".", $keys);
   $v = $array;
   while (sizeof($ks)>0) {
       $k = array_shift($ks);
       if (is_array($v) && isset($v[$k])) {
           $v = $v[$k];
       } else {
           throw new Exception("No such index ".$k);
       }
   }
   return $v;
}


This post has been edited by angch: Aug 19 2016, 07:23 PM
TSjason991
post Aug 19 2016, 07:50 PM

is all about the money, money, money
******
Senior Member
1,189 posts

Joined: Sep 2007
From: Taman Desa


QUOTE(angch @ Aug 19 2016, 07:23 PM)
https://gist.github.com/angch/87957672a8458...37cbaf49e126a99
CODE
function getValueByPath($array, $keys)
{
   // screw you, php is not javascript, stop traversing structures with "."
   $ks = explode(".", $keys);
   $v = $array;
   while (sizeof($ks)>0) {
       $k = array_shift($ks);
       if (is_array($v) && isset($v[$k])) {
           $v = $v[$k];
       } else {
           throw new Exception("No such index ".$k);
       }
   }
   return $v;
}

*
Thanks bro you are the man, unlike some other super experienced guy rclxms.gif rclxms.gif . Sorry la bro for traversing structures haha

This post has been edited by jason991: Aug 19 2016, 07:50 PM

 

Change to:
| Lo-Fi Version
0.0152sec    0.59    5 queries    GZIP Disabled
Time is now: 29th March 2024 - 12:25 AM