Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
i have this multidimensional array.
Array
(
[0] => Array
(
[firstName] => Hadi
[lastName] => Pratama
[age] => 22
)
[1] => Array
(
[firstName] => Jefri
[lastName] => Ronaldo
[age] => 21
)
[2] => Array
(
[firstName] => Rizky
[lastName] => Aulia
[age] => 20
)
)
i want to put them into indexed array like this.
Array = ({"firstName":"Hadi","lastName":"Pratama","age":"22"},{"firstName":"Jefri","lastName":"Ronaldo","age":"21"},
{"firstName":"Rizky","lastName":"Aulia","age":"20"});
help me please.
Not sure quite what your question is here (please clarify by editing your post).
Your output does not look like a Php array. More like JSON.
When you say remove indexes, could this be from having non sequential 0 counted indexes?
See below:
<?php
$people =
[
[
'name'=>'fred',
'family'=>'flintstones'
],
[
'name'=>'barney',
'family' => 'rubble'
],
[
'name'=> 'wilma',
'family' => 'flintstones'
]
];
var_dump(json_encode($people));
unset($people[1]);
var_dump(json_encode($people));
var_dump(json_encode(array_values($people)));
Output:
string(116) "[{"name":"fred","family":"flintstones"},{"name":"barney","family":"rubble"},{"name":"wilma","family":"flintstones"}]"
string(88) "{"0":{"name":"fred","family":"flintstones"},"2":{"name":"wilma","family":"flintstones"}}"
string(80) "[{"name":"fred","family":"flintstones"},{"name":"wilma","family":"flintstones"}]"
You can use array_values to reindex arrays before encoding to JSON to 'remove' those indexes, and turn into a list of objects.
json_encode() : Returns the JSON representation of a value
echo json_encode($a);
Working example :- https://3v4l.org/EgOtE
Question
Your array already has the correct dimensions, just run json_encode($array); over it and you will have the result you're looking for.
If you want to use an associative array, you'll have to be a bit more creative:
$result = [];
foreach($array as $index) {
$result[$index['firstName']] = $index;
}
var_dump($result);
Result:
Array
(
[Hadi] => Array
(
[firstName] => Hadi
[lastName] => Pratama
[age] => 22
)
[Jefri] => Array
(
[firstName] => Jefri
[lastName] => Ronaldo
[age] => 21
)
[Rizky] => Array
(
[firstName] => Rizky
[lastName] => Aulia
[age] => 20
)
)
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 months ago.
Improve this question
I want the array to be processed into my final array how am I supposed to achieve this? Below is the array that I have which I got from the loop.
Array
(
Array
(
[part_id] => 2338117
[supplier] => COOLDRIVE DISTRIBUTION
[quantity] => 12
)
Array
(
[part_id] => 2338117
[supplier] => ROLAN
[quantity] => 20
)
Array
(
[part_id] => 51154
[supplier] => ROLAN
[quantity] => 20
)
)
into the final array.
Array
(
[COOLDRIVE DISTRIBUTION] => Array
(
[proudctID] => Array
(
[0] => 2338117
)
)
[ROLAN] => Array
(
[productID] => Array
(
[0] => 2338117
[1] => 51154
)
)
)
You can use something like this in your case. Considering your old array as $oldarr
$newarr = array();
foreach($oldarr as $value){
$newarr[$value['supplier']]['productID'][] = $value['part_id'];
}
print_r($newarr);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have the following array:
Array (
[ids] => Array (
[0] => e348ae92
[1] => 193ba701
[2] => 58695854
)
[name] => Array (
[0] => Customers
[1] => Suppliers
[2] => Users
)
[subs] => Array (
[0] => 614
[1] => 65
[2] => 99
)
)
I want to take each array key and turn it into a it's own array e.g.
array(
[0] = array(
[0] => e348ae92
[1] => custommers
[2] => 614
)
[1] = array(
[0] => 193ba701
[1] => Suppliers
[2] => 65
)
[2] = array (
[0] => 58695854
[1] => Users
[2] => 99
)
)
I have looked at array_merge, array_combine and a few other things but I have been unsuccessful so far, any pointers in the right direction would be appreciated.
You can use array_map
$f = array_map(null, $a['ids'], $a['name'], $a['subs']);
print_r($f);
Working example:https://3v4l.org/dDG0Y-
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Our Result:
Array (
[0] => Array ( [0] => Province [1] => Party [2] => Age [3] => Name [4] => Gender )
[1] => Array ( [0] => Quebec [1] => NDP [2] => 22 [3] => Liu, Laurin [4] => Female )
[2] => Array ( [0] => Quebec [1] => Bloc Quebecois [2] => 22 [3] => Mourani, Maria [4] => Female )
)
I want a result looking like this: How to convert like this?
array(
['Province'=>'Quebec','Party'=>'NDP','Age'=>22,'Name'=>'Liu, Laurin','Gender'=>'Female'],
['Province'=>'Quebec','Party'=>'Bloc Quebecois','Age'=>43,'Name'=>'Mourani, Maria','Gender'=>'Female']
)
OR
array(
['Province', 'Party', 'Age', 'Name', 'Gender'],
['Quebec', 'NDP', 22, 'Liu, Laurin', 'Female'],
['Quebec', 'Bloc Quebecois', 43, 'Mourani, Maria', 'Female']
)
Pretty simple using an array_combine() to set the headers for each row, and just walking the array setting those headers:
$headers = array_shift($myArray);
array_walk(
$myArray,
function(&$row) use ($headers) {
$row = array_combine($headers, $row);
}
);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I fetch an array from API using
<?php
$json = json_decode(file_get_contents("API_URL"), TRUE);
echo '<pre>';
print_r($json);
?>
I got that result like that
Array
(
[deals] => Array
(
[0] => Array
(
[activeDa
te] => 1430332361000
[bogo] =>
[categories] => Array
(
[0] => Array
(
[id] => 29057
[name] => Bath & Body
)
[1] => Array
(
[id] => 20733
[name] => Cosmetics
)
[2] => Array
(
[id] => 29190
[name] => Skin Care
)
[3] => Array
(
[id] => 20856
[name] => Fragrances
)
[4] => Array
(
[id] => 29059
[name] => Beauty & Personal Care
)
)
[clearance] =>
[couponCode] => HAPPYMOM
[dealImageUrl] => http://cdn.savings.com/logo/1737578.png
[dealUrl] => http://www.savings.com/m/p/19561077/8306099/c?afsrc=1&up=2015-05-01-05-15
[description] => Go through this link to get Assorted Spring Getaway tote for only $20 on orders $40 or more, save 80%. Restrictions may apply. Limited time offer only or when supplies run out.
[discount] => 1
[exclusive] =>
[freeShipping] =>
[homePageStaffPick] =>
[id] => 3862713
[lastUpdated] => 1430332361000
[merchantDisplayUrl] => http://www.bathandbodyworks.com
[merchantId] => 236514
[merchantImageUrl] => http://cdn.savings.com/logo/1737578.png
[merchantName] => Bath and Body Works
[merchantPageStaffPick] =>
[merchantScore] => 17
[merchantUrl] => http://www.savings.com/m/p/19561077/1742990/c?afsrc=1
[minimumSpend] => 0.00
[mobileMonetized] =>
[monetized] => 1
[printable] =>
[promotion] => 80% Off
[rebate] =>
[scope] => SITE_WIDE
[score] => 579
[siteUrls] => Array
(
[0] => http://www.bathandbodyworks.com
)
[startDate] => 1430290800000
[tip] =>
[title] => Get 80% off Assorted Spring Getaway Tote on Orders Over $40 - Only $20
[validated] =>
[voteDown] => 0
[voteUp] => 0
)
My question is how i get the value of [deals][categories][0][name] ?
I want to store value of categories name.
You have your answer in your question itelf. just a little modifiction needed. Please try this:-
echo $yourarrayname['deals'][0]['categories'][0]['name'];
Note:-since category is on the Zero th index of deals. So put zero index before category index.
You already have your answer. The only thing you need to do is make them literal strings and take the first element in the deals array. So:
echo $json['deals'][0]['categories'][0]['name']
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a shipping API which produces a variable called $quotereply.
print_r ($quotereply) gives me the following output:
Array ( [replycode] => 200 [replymessage] => success [replytype] => quote [quote] => Array ( [services] => Array ( [noofservices] => 2 [service1] => Array ( [name] => TestService1Before12am [description] => Test Service 1 Before 12am [carrier] => Camel [price] => 10 [vat] => 0 [vat_rate] => 0 [insurance_cost] => 0 [insurance_cover] => 0 ) [service2] => Array ( [name] => TestService2Anytime [description] => Test Service 2 Anytime [carrier] => Pigeon [price] => 5 [vat] => 0 [vat_rate] => 0 [insurance_cost] => 0 [insurance_cover] => 0 ) ) ) [custom] => Array ( [data] => [orderid] => ) )
My question is, how can I extract a value such as [noofservices] from this? I can't really get my head round what I'm looking at, is it an array within an array?
Thanks in advance!
Just try with:
$quotereply['quote']['services']['noofservices']
it is multidimensional array so you should follow array index keys]
echo $quotereply["quote"]["services"]["noofservices"];