Sure you are looking for the array_column function.
$newArray = array_column($originalArray,'0');
As said in PHP Manual, with this function you can extract, from an array of arrays, the only information you need. It is normally used with Associative arrays, but you can use a number as parameter and it will work as well.
Answer from Liz on Stack OverflowSure you are looking for the array_column function.
$newArray = array_column($originalArray,'0');
As said in PHP Manual, with this function you can extract, from an array of arrays, the only information you need. It is normally used with Associative arrays, but you can use a number as parameter and it will work as well.
Go for a clear loop, your fellow coders will thank you. It's probably about as fast of faster then any other solution, and clear cut code is preferable over obscure loop-dodging.
Just as an illustration: how many seconds would it take you to see what:
list($b) = call_user_func_array('array_map',array_merge(array(null),$a));
...does?
Does the trick. Don't use it.
Extract values from a multidimensional array using php - Stack Overflow
How to extract/slice from a multidimensional array by its index number?
php - how to separate and extract multidimensional array - Stack Overflow
php - extract array from multidimensional array - Stack Overflow
In general, you can loop through a nested array the same way as you loop through a single-level array. Example:
$myArray = array(
'first-level-first-key' => array(
'second-level-first-key' => 'some value',
'second-level-second-key' => 'another value'
),
'first-level-second-key' => array(
'seond-level-another-key' => 'yet another value'
)
);
foreach($myArray as $first_level_key => $first_level_value) {
echo 'Key: '. $first_level_key .'<br />';
echo 'Values: ';
foreach($first_level_value as $second_level_key => $second_level_value) {
echo $second_level_value .', ';
}
echo '<br /><br />';
}
This will print out:
Key: first-level-first-key
Values: some value, another value,
Key: first-level-second-key
Values: yet another value,
Now you know how to loop through nested arrays. Have fun!
Edit
A little bit more explanation, as an answer to tmyie's comment below: As you can see every element is an array in itself:
$myArray['first-level-first-key'] = array('second-level-first-key' => ... );
The first foreach loops through all the 'first-level' arrays (in my example the elements with key 'first-level-....'). So within that loop, the variable $first_level_value holds an array. The second foreach loops through that, second-level, array. This nesting of loops is virtually endless if you have saved yet another array in that second level element.
Compare it with having a couple of boxes in front of you. With the first loop, you say 'Open up every box in front of me'. Then for every single box you have opened, the nested loop says 'Open up every box I found in that box', and so on.
Although there is most probably something very wrong with your application design if you'd write this, the following example is completely valid:
foreach($myArray as $x) {
foreach(
y) {
foreach(
z) {
foreach(
p) {
// etc
}
}
}
}
Since $v is another array in your foreach, you need to do another foreach:
foreach($results as
v) {
foreach(
timestamp => $tally) {
// do whatever you want
}
}
I'm not giving you a complete code and would prefer to leave that as an exercise for you, but that's how - as per your question - to extract multidimensional array
Hi. I really some help here. I cannot figure out how to extract/slice from a multidimensional array by its index number. For example.
This is the array that I have.
Array
(
[0] => Array
(
[0] => https://codeigniter.com/userguide3/helpers/array_helper.html
[1] => blog-17.jpg
[2] => 0
)
[1] => Array
(
[0] => https://codeigniter.com/userguide3/helpers/array_helper.html
[1] => blog-single2.jpg
[2] => 1
)
[2] => Array
(
[0] => https://www.koding.com/2017/05/add-update-json-file-php.html
[1] => comment13.jpg
[2] => 0
)
)What I need is to be able to get a specificy array via its index. So, if I want the array with index 2, I would get this:
Array
(
[0] => https://www.koding.com/2017/05/add-update-json-file-php.html
[1] => comment13.jpg
[2] => 0
)Thanks!
On PHP 5.5 or later, the simplest solution would be using PHP's built-in array_column() function.
$ids = array_column($arr, 'ID');
$bas = array();
foreach ($foo as $bar) {
$bas[] = $bar['ID'];
}
print_r($bas);
Where $foo would be you original array and $bas the one you want to convert it to.
Since the response is in JSON, you would do something like the following:
$json = json_decode($result);
echo $json->result->success->contact->number;
Of course you should also add error handling, and check to see the object exists, etc.
For reference, I used the documentation outlining the response that is returned when sending a message as specified here: Send Message to Number
The response format (for success):
{
"success":true,
"result":{
"success":[
{
"id":"308",
"device_id":"4",
"message":"hello world!",
"status":"pending",
"send_at":"1414624856",
"queued_at":"0",
"sent_at":"0",
"delivered_at":"0",
"expires_at":"1414634856",
"canceled_at":"0",
"failed_at":"0",
"received_at":"0",
"error":"None",
"created_at":"1414624856",
"contact":{
"id":"14",
"name":"Phyllis Turner",
"number":"+447791064713"
}
}
],
"fails":[
]
}
}
First get the response data and print it, see it's array structure using:
echo "<pre>";
print_r($data);
echo "</pre>";
Based on that, access the data you want.
Looks like you were able to figure it out from the comments and the other answer. However, I should clarify that my comments on the question were slightly misleading, so you can disregard them. I actually assumed class referred to PHP objects, when in fact class was simply a key for a string (the array looks to me like some parsed HTML/XML). I realized this when you posted a var_dump() of the array in the comments.
The issue is that the array structure is fairly complicated with all the nesting going on, so it may be difficult to see what's what. In general, I agree with the other answer; debugging with var_dump() is a great way to check. Here's my take on what the answer might be.
Assuming your array variable is $array:
1.
$array['query']['results']['div'][0]['span']['a']['href']
$array['query']['results']['div'][0]['span']['a']['img']['src']
2.
$array['query']['results']['div'][1]['span']['p']
3.
$array['query']['results']['span'][0]['content']
4.
$array['query']['results']['span'][1]['content']
try this:
if this array is in a variable named $array
1-$array['query']['result']['div'][0]['a']['href']
2-$array['query']['result']['div'][1]['p']
3-$array['query']['result']['span'][0]['class']
To debug and find the desired value, try to do like that,
var_dump($array['query']);
then
var_dump($array['query']['result']);
...etc
In PHP >= 5.5 you can use array_column()
$letters = array_column($originalArray, 'id');
Suggest you to use array_map().
$your_arr = array(
array(
"id" => "c" ,"val" => 290 ),
array(
"id" => "a","val" => 160,
),
array(
"user" => "v","val" => 150,
)
);
$arr = array_map(function($v){
return $v['id'];
}, $your_arr);
print '<pre>';
print_r($arr);
print '</pre>';
Output:
Array
(
[0] => c
[1] => a
[2] => v
)
You can accomplice it by using array_search and array_column
This will give you the multidimensional array key
$key = array_search("JT", array_column($json['stock'], 'operator'));
Then you can do
$sims = $json['stock'][$key]['sims'];
print_r($sims) //this will print desired array
Try:
$json = json_decode($stock, true);
foreach($json["stock"] as $arr){
echo $arr["operator"]."\n";
foreach($arr["sims"] as $sim){
echo $sim."\n";
}
echo "\n";
}
This would output (for example):
ECL
8944122616994
89650264517182
894412265075
894412JT
89445023065
894156673081
8944501
89445027
$user = $_SESSION['user'];
echo $user->id;
This seems to be what you need.
This would output the user name: (if there was any)
print $_SESSION["__default"]["user"]->name;
You first need to overcome the __default key index within the $_SESSION array. Likewise is user an array key. But what the user entry contains is an object, so you need the -> object arrow thingy to finally access the name.
Also I would not rely on __default being a given. Don't know anything about Joomla, but the $_SESSION array structure might be more flexible. Hencewhy it's advisable to find a function which instead fetches the user object for you.