How to retrieve data from index of an array in php? [duplicate] - php

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 3 years ago.
It is my array:
[main] => Array (
[data] => Array
( [777] => Array (... ),
[888] => Array (....),
[999] => Array (....) ));
I usually use such syntax:
array['main']['data'][0]...
however here I don't know what exactly values are (777, 888, 999).
How could I receive this 777, 888, 999 and also theirs included data?

You can use foreach loop and you can access the values in each iteration like below :
<?php
foreach($array['main']['data'] as $item) {
print_r($item['name']);
// i assure there is some index as name in the subarray. Change the index value as per your data.
}
?>
Refer the document ion for further information PHP Foreach loop

Use foreach loop... Foreach loop. Will solve your problem

Related

PHP Get array of a property from array of objects [duplicate]

This question already has answers here:
How to extract and access data from JSON with PHP?
(1 answer)
PHP JSON Specific Key To Array [duplicate]
(3 answers)
Closed 2 years ago.
I have an API that sends me the data in the format
[{"part_no":"AAA"},{"part_no":"BBB"},{"part_no":"CCC"},......{"part_no":"ZZZ"}]
I want to create an array like ["AAA", "BBB", ...., "ZZZ"] from the above array.
I know it's possible by iterating the array item by item, and appending it to a new array, but thought that there might be a better (and hopefully faster) approach.
Like C# hasLinq that does this all in a one-liner, JS has map, it is possible to do a similar thing in PHP ?
<?php
$json = '[{"part_no":"AAA"},{"part_no":"BBB"},{"part_no":"CCC"},{"part_no":"ZZZ"}]';
$data = json_decode($json, true);
$result = array_column($data, 'part_no');
var_export($result);
Output:
array (
0 => 'AAA',
1 => 'BBB',
2 => 'CCC',
3 => 'ZZZ',
)

PHP Pull data out of an array [duplicate]

This question already has answers here:
How to get single value from this multi-dimensional PHP array [duplicate]
(6 answers)
Closed 5 years ago.
I know this should be a relatively simple thing but I have not been able to do it yet. I have look hi and low and every example I try it fails I am sure it is fairly simple
Here is my array. I need to get the value of name last and filenames
Any help would be most appreciated.
Thanks!!
Array
(
[formData] => Array
(
[name] => TEST
[last] => TEST1
[filenames] => Array
(
[0] => /ocdata/uploads/export-1511887767.csv
)
)
)
Really simple method to see your content:
foreach($array as $k => $v)
{
echo $k . $v . PHP_EOL; // see content of your array
}
Or use directly the values:
$array['formData']['name'];
$array['formData']['last'];
$array['formData']['filenames'];

PHP - Find key in multidimensional array [duplicate]

This question already has answers here:
PHP multidimensional array search by value
(23 answers)
Closed 3 years ago.
Is there any predefined PHP function to find a key in a multi dimensional array?
In the below example - there is a variable name 'rose' and I need to get the key of the array by using the variable name.
The result of the key is "flower".
$array = array (
'fruits' => array (
'mango',
'cherry'
),
'flowers' => array (
'rose'
)
);
How do I achieve this?
Loop it up using a foreach
$keyword='mango';
foreach($array as $k=>$arr)
{
if(in_array($keyword,$arr))
{
echo $k;break;// "prints" fruits
}
}
Working Demo

Getting the array index in a foreach loop [duplicate]

This question already has answers here:
How to find the foreach index?
(14 answers)
Closed 8 years ago.
So I may just be completely overcomplicating this for myself but I am trying to get the index of an array in a multidimensional array.
Hopefully showing it makes more sense.
Array
(
[1234] => Array
(
[Name] => Test
)
[5435] => Array
(
[Name] => Test
)
)
I have a large array with different numbers as the index and I need to do a foreach through them, but I need that index number. (1234,5435)
Is there any easy way of doing this?
Yes there is.
foreach ($arr as $key=>$val)
{
//do stuff
}
The $key is the key you need

How to get the value of an array that's inside another array [duplicate]

This question already has answers here:
Get the first element of an array
(39 answers)
multidimensional array
(3 answers)
Closed 9 years ago.
I have this:
Array
(
[28] => Array
(
[name] => HTC Touch HD
)
)
There's only one array inside the main array and I only the value of name. Problem is that I don't know the index (28).
You could use array_values just in general to get rid of any weird keys:
$normal = array_values($arr);
$normal[0]['name']
Or in this particular case, end, which is only a little bit hacky:
end($normal)['name']
http://codepad.viper-7.com/cApBjK
(Yep, reset and first and such work too.)
You could also just use
$array = array_pop($array);
And then to get the name element:
$array['name']
You can try something like this:
reset($outerArray);
$innerArray = current($outerArray);
Now you should have access to the value you want.
Pretty self-explanatory :)
<?php
$array = array(
28 => array(
'name' => 'HTC Touch HD'
)
);
$key = current(array_keys($array));
echo '<pre>';
print_r($array[$key]);
echo '</pre>';
?>
If you don't know the structure of an array, you can use foreach construct.

Categories