Related
I am currently stuck in json formatting for php. I have given my outputted json below. What I need to do is to make the format of the current json to the desired one. I am missing the arrays in the JSON format. Can anyone help me on this.
My code to print the json output is below:
$menuHead=array();
$i=0;
foreach($res as $key => $value){
$i=$key+1;
//$menuHead[$i]['menuHead']=$value['category'];
if(isset($menuHead[$key]['menuHead'])){
if($menuHead[$key]['menuHead']==$value['category']){
$menuHead[$key]['data'][$i]['itemName']=$value['sub_category'];
$menuHead[$key]['data'][$i]['price']=$value['price'];
$menuHead[$key]['data'][$i]['description']=$value['description'];
$menuHead[$key]['data'][$i]['itemId']=$value['id'];
$menuHead[$key]['data'][$i]['customizable']=$value['customizable'];
}else{
$menuHead[$i]['menuHead']=$value['category'];
$menuHead[$i]['data'][$i]['itemName']=$value['sub_category'];
$menuHead[$i]['data'][$i]['price']=$value['price'];
$menuHead[$i]['data'][$i]['description']=$value['description'];
$menuHead[$i]['data'][$i]['itemId']=$value['id'];
$menuHead[$i]['data'][$i]['customizable']=$value['customizable'];
}
}else{
$menuHead[$i]['menuHead']=$value['category'];
$menuHead[$i]['data'][$i]['itemName']=$value['sub_category'];
$menuHead[$i]['data'][$i]['price']=$value['price'];
$menuHead[$i]['data'][$i]['description']=$value['description'];
$menuHead[$i]['data'][$i]['itemId']=$value['id'];
$menuHead[$i]['data'][$i]['customizable']=$value['customizable'];
}
}
$final['MenuList']=$menuHead;
echo json_encode($final);
Current format:
{
"MenuList": {
"1": {
"menuHead": "Main Course",
"data": {
"1": {
"itemName": "Chicken Thai Curry",
"price": "599",
"description": "",
"itemId": "67",
"customizable": "1"
}
}
},
"2": {
"menuHead": "Refreshments",
"data": {
"2": {
"itemName": "Kingfisher Premium",
"price": "999",
"description": "Kingfisher beer",
"itemId": "69",
"customizable": "1"
},
"3": {
"itemName": "Mocktail",
"price": "999",
"description": "",
"itemId": "68",
"customizable": "1"
}
}
},
"4": {
"menuHead": "Rice biriyani",
"data": {
"4": {
"itemName": "Dal makni risotto",
"price": "499",
"description": "Dal makhni risotto",
"itemId": "66",
"customizable": "1"
}
}
}
}
}
Desired Format:
{
"menuList": [
{
"menuHead": "In Steamer (Momos)",
"data": [
{
"itemName": "Tandoori Momo",
"description": "",
"price": "150",
"itemId": "16",
"customizable": "0"
},
{
"itemName": "Fried Momo Pork",
"price": "100",
"description": "",
"itemId": "15",
"customizable": "0"
}
]
},
{
"itemName": "Rice and Noodles",
"data": [
{
"sub_category": "Tandoori Momo",
"description": "",
"price": "150",
"itemId": "16",
"customizable": "0"
},
{
"itemName": "Fried Momo Pork",
"price": "100",
"description": "",
"itemId": "15",
"customizable": "0"
}
]
}
]
}
Raw response is below:
array(4) { [0]=> array(7) { ["id"]=> string(2) "67" ["restaurant_id"]=> string(1) "5" ["category"]=> string(11) "Main Course" ["sub_category"]=> string(18) "Chicken Thai Curry" ["price"]=> string(3) "599" ["description"]=> string(0) "" ["customizable"]=> string(1) "1" } [1]=> array(7) { ["id"]=> string(2) "69" ["restaurant_id"]=> string(1) "5" ["category"]=> string(12) "Refreshments" ["sub_category"]=> string(18) "Kingfisher Premium" ["price"]=> string(3) "999" ["description"]=> string(15) "Kingfisher beer" ["customizable"]=> string(1) "1" } [2]=> array(7) { ["id"]=> string(2) "68" ["restaurant_id"]=> string(1) "5" ["category"]=> string(12) "Refreshments" ["sub_category"]=> string(8) "Mocktail" ["price"]=> string(3) "999" ["description"]=> string(0) "" ["customizable"]=> string(1) "1" } [3]=> array(7) { ["id"]=> string(2) "66" ["restaurant_id"]=> string(1) "5" ["category"]=> string(13) "Rice biriyani" ["sub_category"]=> string(17) "Dal makni risotto" ["price"]=> string(3) "499" ["description"]=> string(18) "Dal makhni risotto" ["customizable"]=> string(1) "1" } }
If you want a javascript compatible array, the index must start at 0. The easiest way to do that, is to use array_values():
$final['MenuList'] = array_values($menuHead);
The problem is that when your adding the data items, you need to add them without specific keys, as you add them with $i as in...
$menuHead[$key]['data'][$i]['itemName']=$value['sub_category'];
This will stop them being a normal array as you want it to be. For json_encode() an array must start at 0 and be sequential for it to be an array.
Instead create them in one go and add them to the end of the existing data using []...
$menuHead[$key]['data'][] = ['itemName' =>$value['sub_category'],
'price'=> $value['price'],
'description'=>$value['description'],
'itemId'=>$value['id'],
'customizable'=>$value['customizable']];
This needs to be done with each set of similar code, which includes the overall array itself, this can be done using
$final['MenuList'] = array_values($menuHead);
To try and fix the data you already have, which means no changes except adding the following code...
foreach ( $menuHead as $menu ) {
$menu['data'] = array_values($menu['data']);
}
$final['MenuList'] = array_values($menuHead);
Use array_values();
I fixed your code, it should work
$menuHead=array();
$i=0;
foreach($res as $key => $value){
$i=$key+1;
//$menuHead[$i]['menuHead']=$value['category'];
if(isset($menuHead[$key]['menuHead'])){
if($menuHead[$key]['menuHead']==$value['category']){
$menuHead[$key]['data'][$i]['itemName']=$value['sub_category'];
$menuHead[$key]['data'][$i]['price']=$value['price'];
$menuHead[$key]['data'][$i]['description']=$value['description'];
$menuHead[$key]['data'][$i]['itemId']=$value['id'];
$menuHead[$key]['data'][$i]['customizable']=$value['customizable'];
}else{
$menuHead[$i]['menuHead']=$value['category'];
$menuHead[$i]['data'][$i]['itemName']=$value['sub_category'];
$menuHead[$i]['data'][$i]['price']=$value['price'];
$menuHead[$i]['data'][$i]['description']=$value['description'];
$menuHead[$i]['data'][$i]['itemId']=$value['id'];
$menuHead[$i]['data'][$i]['customizable']=$value['customizable'];
}
}else{
$menuHead[$i]['menuHead']=$value['category'];
$menuHead[$i]['data'][$i]['itemName']=$value['sub_category'];
$menuHead[$i]['data'][$i]['price']=$value['price'];
$menuHead[$i]['data'][$i]['description']=$value['description'];
$menuHead[$i]['data'][$i]['itemId']=$value['id'];
$menuHead[$i]['data'][$i]['customizable']=$value['customizable'];
}
}
// i'am use array_values()
$final['MenuList']= array_values($menuHead);
echo json_encode($final);
I need help in order to manipulate a multidimensional array during PHP foreach loop. I have the array below:
$sports = [
[
"id" => "soccer",
"name" => "Football",
"categories" => [
[
"id" => "s1",
"name" => "category 1",
"sportID" => "soccer"
],
[
"id" => "s2",
"name" => "category 2",
"sportID" => "soccer"
],
],
"img" => "/test.png"
],
[
"id" => "tennis",
"name" => "Tennis",
"categories" => [
[
"id" => "t1",
"name" => "category 1",
"sportID" => "tennis"
],
[ "id" => "t2",
"name" => "category 2",
"sportID" => "tennis"
],
],
"img" => "/test.png"
],
];
I have the below foreach in order to take all the sports with the corresponding categories for every sport,
foreach($sports as $s){
$categories = $s['categories'];
foreach($categories as $c){
$cats[] = [
"id" => $c['id'],
"name" => $c['name'],
"sportID" => $s['id'],
];
}
$allCategories[] = $cats;
$data[] = [
"id" => $s['id'],
"name" => $s['name'],
"categories" => $cats,
"img" => $s['img'],
];
$output[] = $data;
}
but the output is not what I expected; instead, I am getting repeated results like the one below:
array(2) {
[0]=>
array(1) {
[0]=>
array(4) {
["id"]=>
string(8) "soccer"
["name"]=>
string(8) "Football"
["categories"]=>
array(2) {
[0]=>
array(3) {
["id"]=>
string(2) "s1"
["name"]=>
string(10) "category 1"
["sportID"]=>
string(8) "soccer"
}
[1]=>
array(3) {
["id"]=>
string(2) "s2"
["name"]=>
string(10) "category 2"
["sportID"]=>
string(8) "soccer"
}
}
["img"]=>
string(9) "/test.png"
}
}
[1]=>
array(2) {
[0]=>
array(4) {
["id"]=>
string(8) "soccer"
["name"]=>
string(8) "Football"
["categories"]=>
array(2) {
[0]=>
array(3) {
["id"]=>
string(2) "s1"
["name"]=>
string(10) "category 1"
["sportID"]=>
string(8) "soccer"
}
[1]=>
array(3) {
["id"]=>
string(2) "s2"
["name"]=>
string(10) "category 2"
["sportID"]=>
string(8) "soccer"
}
}
["img"]=>
string(9) "/test.png"
}
[1]=>
array(4) {
["id"]=>
string(10) "tennis"
["name"]=>
string(8) "Tennis"
["categories"]=>
array(4) {
[0]=>
array(3) {
["id"]=>
string(2) "s-1"
["name"]=>
string(10) "category 1"
["sportID"]=>
string(8) "soccer"
}
[1]=>
array(3) {
["id"]=>
string(2) "s2"
["name"]=>
string(10) "category 2"
["sportID"]=>
string(8) "soccer"
}
[2]=>
array(3) {
["id"]=>
string(2) "t1"
["name"]=>
string(10) "category 1"
["sportID"]=>
string(10) "tennis"
}
[3]=>
array(3) {
["id"]=>
string(2) "t2"
["name"]=>
string(10) "category 2"
["sportID"]=>
string(10) "tennis"
}
}
["img"]=>
string(9) "/test.png"
}
}
}
As you can imagine this is not the correct output, as in the categories of the tennis you can see the categories of the soccer also.
How can I correct my foreach loop in order to get the correct output?
You forget to reset $cats and $data arrays.
<?php
$sports = [
[
"id" => "soccer",
"name" => "Football",
"categories" => [
[
"id" => "s1",
"name" => "category 1",
"sportID" => "soccer"
],
[
"id" => "s2",
"name" => "category 2",
"sportID" => "soccer"
],
],
"img" => "/test.png"
],
[
"id" => "tennis",
"name" => "Tennis",
"categories" => [
[
"id" => "t1",
"name" => "category 1",
"sportID" => "tennis"
],
[ "id" => "t2",
"name" => "category 2",
"sportID" => "tennis"
],
],
"img" => "/test.png"
],
];
foreach($sports as $s){
$categories = $s['categories'];
# before inner loop we need to reset both arrays
$cats = [];
$data = [];
foreach($categories as $c){
$cats[] = [
"id" => $c['id'],
"name" => $c['name'],
"sportID" => $s['id'],
];
}
$allCategories[] = $cats;
$data[] = [
"id" => $s['id'],
"name" => $s['name'],
"categories" => $cats,
"img" => $s['img'],
];
$output[] = $data;
}
echo '<PRE>';
print_r($output);
I have data saved in JSON format (Prestashop) - I need to get to that data - which is deeply nested in arrays.
Here is the function:
public static function getAllCustomizedDatas($id_cart, $id_lang = null, $only_in_cart = true, $id_shop = null)
{
$datas = parent::getAllCustomizedDatas($id_cart, $id_lang, $only_in_cart, $id_shop);
var_dump($datas);
/*
* Iterate over $datas, you're looking for
* [id_product][id_product_attribute][id_address_delivery][id_customization][datas]
* Datas will contain an array of fields broken by their type. You can then decode
* the ones that need to be decoded and return the result:
*/
return $datas;
}
if I var_dump $datas I see this (I formatted this to make it a little easier for myself to read):
array(1) {
[8]=> array(1) {
[0]=> array(1) {
[0]=> array(2) {
[22]=> array(4) {
["datas"]=> array(1) {
[1]=> array(1) {
[0]=> array(9) {
["id_customization"]=> string(2) "22"
["id_address_delivery"]=> string(1) "0"
["id_product"]=> string(1) "8"
["id_customization_field"]=> string(1) "2"
["id_product_attribute"]=> string(1) "0"
["type"]=> string(1) "1"
["index"]=> string(1) "2"
["value"]=> string(615) "[[{"name":"item[1][line1]","customization":"asdf"},{"name":"item[1][line2]","customization":""},{"name":"item[1][line3]","customization":""},{"name":"item[1][line4]","customization":""},{"name":"item[1][line5]","customization":""},{"name":"item[1][line6]","customization":""},{"name":"item[1][line7]","customization":""}],[{"name":"item[2][line1]","customization":"asdf"},{"name":"item[2][line2]","customization":""},{"name":"item[2][line3]","customization":""},{"name":"item[2][line4]","customization":""},{"name":"item[2][line5]","customization":""},{"name":"item[2][line6]","customization":""},{"name":"item[2][line7]","customization":""}]]"
["name"]=> string(20) "Client Customization"
}
}
}
["quantity"]=> int(2)
["quantity_refunded"]=> int(0)
["quantity_returned"]=> int(0)
}
[23]=> array(4) {
["datas"]=> array(1) {
[1]=> array(1) {
[0]=> array(9) {
["id_customization"]=> string(2) "23"
["id_address_delivery"]=> string(1) "0"
["id_product"]=> string(1) "8"
["id_customization_field"]=> string(1) "2"
["id_product_attribute"]=> string(1) "0"
["type"]=> string(1) "1"
["index"]=> string(1) "2"
["value"]=> string(615) "[[{"name":"item[1][line1]","customization":"asdf"},{"name":"item[1][line2]","customization":""},{"name":"item[1][line3]","customization":""},{"name":"item[1][line4]","customization":""},{"name":"item[1][line5]","customization":""},{"name":"item[1][line6]","customization":""},{"name":"item[1][line7]","customization":""}],[{"name":"item[2][line1]","customization":"asdf"},{"name":"item[2][line2]","customization":""},{"name":"item[2][line3]","customization":""},{"name":"item[2][line4]","customization":""},{"name":"item[2][line5]","customization":""},{"name":"item[2][line6]","customization":""},{"name":"item[2][line7]","customization":""}]]"
["name"]=> string(20) "Client Customization"
}
}
}
["quantity"]=> int(2)
["quantity_refunded"]=> int(0)
["quantity_returned"]=> int(0)
}
}
}
}
}
What will be the easiest way to get to the ["value"] portion of the deeply nested array?
This has to be fairly dynamic because depending on the amount of items this user has - the amount of arrays will change. In this example, there are 2 there (each with 2 items in 'value'). A user could add 3 or 4 or 10 items if they wanted to. But I'm just trying to get to ['value'] and convert that JSON into HTML for the view this is getting passed to.
Bonus: Know of a way to easily iterate through the JSON data?
The JSON data looks like this:
[
[{
"name": "item[1][line1]",
"customization": "asdf"
}, {
"name": "item[1][line2]",
"customization": ""
}, {
"name": "item[1][line3]",
"customization": ""
}, {
"name": "item[1][line4]",
"customization": ""
}, {
"name": "item[1][line5]",
"customization": ""
}, {
"name": "item[1][line6]",
"customization": ""
}, {
"name": "item[1][line7]",
"customization": ""
}],
[{
"name": "item[2][line1]",
"customization": "asdf"
}, {
"name": "item[2][line2]",
"customization": ""
}, {
"name": "item[2][line3]",
"customization": ""
}, {
"name": "item[2][line4]",
"customization": ""
}, {
"name": "item[2][line5]",
"customization": ""
}, {
"name": "item[2][line6]",
"customization": ""
}, {
"name": "item[2][line7]",
"customization": ""
}]
]
I ended up just nesting a bunch of foreach statements until I had arrived at the array I wanted to. It was ugly, but functional.
Then I did json_decode across the JSON and iterated across it.
https://packagist.org/packages/ishworkh/multi-level-array-iterator
This might be helpful in iterating through deep nested array. And then use key or hierarchy information to filter out value needed for you.
Lets say this is your array
$array = [
[
[
[
[
"datas" => [
[
[
"id_customization" => "22",
"id_address_delivery" => "0",
"id_product" => "8",
"id_customization_field" => "2",
"id_product_attribute" => "0",
"type" => "1",
"index" => "2",
"value" => '[[{"name":"item[1][line1]","customization":"asdf"},{"name":"item[1][line2]","customization":""},{"name":"item[1][line3]","customization":""},{"name":"item[1][line4]","customization":""},{"name":"item[1][line5]","customization":""},{"name":"item[1][line6]","customization":""},{"name":"item[1][line7]","customization":""}],[{"name":"item[2][line1]","customization":"asdf"},{"name":"item[2][line2]","customization":""},{"name":"item[2][line3]","customization":""},{"name":"item[2][line4]","customization":""},{"name":"item[2][line5]","customization":""},{"name":"item[2][line6]","customization":""},{"name":"item[2][line7]","customization":""}]]',
"name" => "Client Customization",
],
],
],
"quantity" => 2,
"quantity_refunded" => 0,
"quantity_returned" => 0,
],
[
[
[
[
"id_customization" => "23",
"id_address_delivery" => "0",
"id_product" => "8",
"id_customization_field" => "2",
"id_product_attribute" => "0",
"type" => "1",
"index" => "2",
"value" => '[[{"name":"item[1][line1]","customization":"asdf"},{"name":"item[1][line2]","customization":""},{"name":"item[1][line3]","customization":""},{"name":"item[1][line4]","customization":""},{"name":"item[1][line5]","customization":""},{"name":"item[1][line6]","customization":""},{"name":"item[1][line7]","customization":""}],[{"name":"item[2][line1]","customization":"asdf"},{"name":"item[2][line2]","customization":""},{"name":"item[2][line3]","customization":""},{"name":"item[2][line4]","customization":""},{"name":"item[2][line5]","customization":""},{"name":"item[2][line6]","customization":""},{"name":"item[2][line7]","customization":""}]]',
"name" => "Client Customization",
],
],
],
"quantity" => 2,
"quantity_refunded" => 0,
"quantity_returned" => 0,
],
],
],
],
];
You could create a function like this, that uses multi-level-array-iterator's iterate method to go over all the nested array and
yield out found values.
/**
* #param array $nestedArray
*
* #return Generator
*/
function extractValuesFromArray(array $nestedArray):Generator
{
// $key is the local index key, which in this case should be 'value' we are looking to filter for
foreach(\ArrayIterator\ArrayIteratorFacade::iterate($nestedArray) as $key => $ArrayElement)
{
if ('value' === $key)
{
yield $ArrayElement->getValue();
}
}
}
var_dump(iterator_to_array(extractValuesFromArray($array)));
should give
array(2) {
[0]=>
string(643) "[[{"name":"item[1][line1]","customization":"asdf"},{"name":"item[1][line2]","customization":""},{"name":"item[1][line3]","customization":""},{"name":"item[1][line4]","customization":""},{"name":"item[1][line5]","customization":""},{"name":"item[1][line6]","customization":""},{"name":"item[1][line7]","customization":""}],[{"name":"item[2][line1]","customization":"asdf"},{"name":"item[2][line2]","customization":""},{"name":"item[2][line3]","customization":""},{"name":"item[2][line4]","customization":""},{"name":"item[2][line5]","customization":""},{"name":"item[2][line6]","customization":""},{"name":"item[2][line7]","customization":""}]]"
[1]=>
string(643) "[[{"name":"item[1][line1]","customization":"asdf"},{"name":"item[1][line2]","customization":""},{"name":"item[1][line3]","customization":""},{"name":"item[1][line4]","customization":""},{"name":"item[1][line5]","customization":""},{"name":"item[1][line6]","customization":""},{"name":"item[1][line7]","customization":""}],[{"name":"item[2][line1]","customization":"asdf"},{"name":"item[2][line2]","customization":""},{"name":"item[2][line3]","customization":""},{"name":"item[2][line4]","customization":""},{"name":"item[2][line5]","customization":""},{"name":"item[2][line6]","customization":""},{"name":"item[2][line7]","customization":""}]]"
}
Below is the result and I want to remove duplicate from the array
I tried using this code: $login_data1['items'] = array_values(array_map("unserialize", array_unique(array_map("serialize", $login_data1['items']))));
{
"items": [
{
"id": "2",
"tags": [
{
"name": "Microsoft"
}
],
"type": "manual",
},
{
"id": "1",
"tags": [
{
"name": "Snow Leopard"
}
],
"type": "faq"
},
{
"id": "2",
"tags": [
{
"name": "Microsoft"
}
],
"type": "manual"
}
],
}
I tried using $login_data1['items'] = array_unique($login_data1['items'] ,SORT_REGULAR); but this adds serial numbers at the each json response
Try as using array_unique
$json = '{
"items": [
{
"id": "2",
"tags": [
{
"name": "Microsoft"
}
],
"type": "manual"
},
{
"id": "1",
"tags": [
{
"name": "Snow Leopard"
}
],
"type": "faq"
},
{
"id": "2",
"tags": [
{
"name": "Microsoft"
}
],
"type": "manual"
}
]
}';
foreach(json_decode($json, true) as $key => $value){
$input = array_unique($value,SORT_REGULAR);
}
If its an array then simply use
array_unique($login_data['items'],SORT_REGULAR);
Fiddle
array_unique works perfectly if you pass a multidimensional array.
$login_data1['items'] = array_unique($login_data1['items'], SORT_REGULAR);
It doesn't work with your json because it's an array of object. Infact:
$array = json_decode($json);
var_dump($array);
returns:
object(stdClass)#1 (1) {
["items"]=>
array(3) {
[0]=>
object(stdClass)#2 (3) {
["id"]=>
string(1) "2"
["tags"]=>
array(1) {
[0]=>
object(stdClass)#3 (1) {
["name"]=>
string(9) "Microsoft"
}
}
["type"]=>
string(6) "manual"
}
[1]=>
object(stdClass)#4 (3) {
["id"]=>
string(1) "1"
["tags"]=>
array(1) {
[0]=>
object(stdClass)#5 (1) {
["name"]=>
string(12) "Snow Leopard"
}
}
["type"]=>
string(3) "faq"
}
[2]=>
object(stdClass)#6 (3) {
["id"]=>
string(1) "2"
["tags"]=>
array(1) {
[0]=>
object(stdClass)#7 (1) {
["name"]=>
string(9) "Microsoft"
}
}
["type"]=>
string(6) "manual"
}
}
}
Your json should look like this:
{
"items": [
{
"id": "2",
"tags": {
"name": "Microsoft"
},
"type": "manual"
},
{
"id": "1",
"tags": {
"name": "Snow Leopard"
},
"type": "faq"
},
{
"id": "2",
"tags": {
"name": "Microsoft"
},
"type": "manual"
},
{
"id": "2",
"tags": {
"name": "Microsoft"
},
"type": "manual"
}
]
}
And now:
$array = json_decode($json);
var_dump($array);
returns:
array(1) {
["items"]=>
array(4) {
[0]=>
array(3) {
["id"]=>
string(1) "2"
["tags"]=>
array(1) {
["name"]=>
string(9) "Microsoft"
}
["type"]=>
string(6) "manual"
}
[1]=>
array(3) {
["id"]=>
string(1) "1"
["tags"]=>
array(1) {
["name"]=>
string(12) "Snow Leopard"
}
["type"]=>
string(3) "faq"
}
[2]=>
array(3) {
["id"]=>
string(1) "2"
["tags"]=>
array(1) {
["name"]=>
string(9) "Microsoft"
}
["type"]=>
string(6) "manual"
}
[3]=>
array(3) {
["id"]=>
string(1) "2"
["tags"]=>
array(1) {
["name"]=>
string(9) "Microsoft"
}
["type"]=>
string(6) "manual"
}
}
}
And array_unique works.
I got the solution for this.
$login_data1['items'] =array_values(array_unique($login_data1['items'] ,SORT_REGULAR));
Try array_value and array_unique together and serial number will be removed!
$login_data1['items'] =array_values(array_unique($login_data1['items'] ,SORT_REGULAR));
array {
[0]=> array { [0]=> "1" [1]=> "7" [2]=> "5" [3]=> "0" [4]=> "0" }
[1]=> array { [0]=> "2" [1]=> "3" [2]=> "7" [3]=> "0" [4]=> "0" }
[2]=> array { [0]=> "3" [1]=> "5" [2]=> "10" [3]=> "0" [4]=> "0" }
[3]=> array { [0]=> "4" [1]=> "11" [2]=> "4" [3]=> "0" [4]=> "0" }
[4]=> array { [0]=> "5" [1]=> "12" [2]=> "9" [3]=> "0" [4]=> "0" }
[5]=> array { [0]=> "6" [1]=> "6" [2]=> "12" [3]=> "0" [4]=> "0" }
[6]=> array { [0]=> "7" [1]=> "8" [2]=> "6" [3]=> "0" [4]=> "0" }
[7]=> array { [0]=> "8" [1]=> "0" [2]=> "14" [3]=> "0" [4]=> "0" }
[8]=> array { [0]=> "9" [1]=> "25" [2]=> "8" [3]=> "0" [4]=> "0" }
[9]=> array { [0]=> "10" [1]=> "30" [2]=> "7" [3]=> "0" [4]=> "0" }
}
i would like to ask how can i use list() function to list out
array{7,3,5,11,12,6,8,0,25,30}
thank you very much.
$yourarray = array();
foreach($array as $arr)
{
$yourarray[] = $arr[1];
}
print_r($yourarray);
list() doesn't work that way, it assigns a one-dimensional array of size n to n variables. The way this data is structured, the only way I can see to extract that data is a foreach, as others have suggested.
The "modern" / functional way to isolate a single "column" of data from a two multidimensional array is to call array_column() and write the desired column's key as the second parameter.
Code: (Demo)
$array = [
["1", "7", "5", "0", "0"],
["2", "3", "7", "0", "0"],
["3", "5", "10", "0", "0"],
["4", "11", "4", "0", "0"],
["5", "12", "9", "0", "0"],
["6", "6", "12", "0", "0"],
["7", "8", "6", "0", "0"],
["8", "0", "14", "0", "0"],
["9", "25", "8", "0", "0"],
["10", "30", "7", "0", "0"],
];
var_export(array_column($array, 1));
Output:
array (
0 => '7',
1 => '3',
2 => '5',
3 => '11',
4 => '12',
5 => '6',
6 => '8',
7 => '0',
8 => '25',
9 => '30',
)