Converting string with keys and values to array [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
unserialize problem
I have a string in the form of:
a:16:{i:0;s:3:"696";i:1;s:3:"698";i:2;s:3:"690";}"
I am looking at turning this back into array, so that it will be along the lines of:
array(16) {
0 => 696,
1 => 698,
2 => 690
}
Any ideas how to do this?
Thanks

It looks like a serialized PHP string, try
$array = unserialize($value);
Manual: http://php.net/manual/en/function.unserialize.php
Update
The string contains a flaw, as it expects a array of 16 elements, but only 3 given.
Consider:
$a = array (
0 => '696',
1 => '698',
2 => '690'
);
$s = serialize($a);
will result in:
"a:3:{i:0;s:3:"696";i:1;s:3:"698";i:2;s:3:"690";}"

Use the unserialize() function.
$array = unserialize($serialized_string);

Related

How to use array_map with reset callback in php 8 [duplicate]

This question already has answers here:
Change the array KEY to a value from sub array
(7 answers)
How can i get sub array value to main array key? [Php] [duplicate]
(2 answers)
Multidimensional indexed array to associative array depending on column value
(2 answers)
Closed 10 months ago.
I have a function that work good in php 7 but in php 8.0.11 has warning
$orders->result = array_map('reset', $orders->result);
E_WARNING: reset(): Argument #1 ($array) must be passed by reference, value given in
Of course, I can use this code instead that but maybe slower than array_map
foreach ($orders->result as $item)
$result[$item[0]['order_id']] = $item[0];
Edit:
before foreach or array_map the output like this
Array
(
[0] => Array
(
[order_id] => 41111909
[shop_id] => 34277
[user_id] => 42363
[status_id] => 4
)
)
after use foreach output like this
Array
(
[41111909] => Array
(
[order_id] => 41111909
[shop_id] => 34277
[user_id] => 42363
[status_id] => 4
)
)
How to solve it ?
The simplest replacement is to make an arrow function (single-expression closure) to get the element for you.
If the arrays are plain lists, so that you're always getting element 0, that's as simple as:
$orders->result = array_map(fn($item) => $item[0], $orders->result);
Another alternative in this case is array_column:
$orders->result = array_colum($orders->result, 0);
If you have other keys, you could use either reset or array_key_first in the callback instead:
$orders->result = array_map(fn($item) => $item[array_key_first($item)], $orders->result);
(I suspect array_key_first will be slightly more efficient, since it doesn't need to manipulate the internal array pointer.)
It's not entirely clear what you're trying to do, but it seems like you are trying to get the first value from nested arrays. You should be able to use a function:
$orders->result = array_map(function($v) { return reset($v); }, $orders->result);
You may also try using current if it does what you need as it doesn't take a reference and the array pointer should already be reset by array_map:
$orders->result = array_map('current', $orders->result);
After the edit the code in question is not re-indexing the array as you show in the before and after. To do that is simple:
$orders->result = array_column($orders->result, null, 'order_id');

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',
)

Array display without key [duplicate]

This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 6 years ago.
I am trying to display an array without key item. Just want to display array values without key.
Here is my sample code I tried
$myList = array(
0 =>array(
"product_id"=> 8085
),
1 =>array(
"product_id"=> 8087
),
2 =>array(
"product_id"=> 8086
),
3 =>array(
"product_id"=> 8042
),
);
$newList = array();
foreach($myList as $listItem) {
$newList[] = $listItem['product_id'];
}
$a=array();
$a= array_values($newList);
print_r($a);
I want my array like this
$productIds = array(8085,8087,8086,8042);
Here is my sample code link
You're looking for array_column (which is available as of PHP 5.5):
$productIds = array_column($myList, 'product_id');
This gives you:
var_export($productIds);
array (
0 => 8085,
1 => 8087,
2 => 8086,
3 => 8042,
)
Which is exactly what you want:
var_dump($productIds === array(8085,8087,8086,8042)); // bool(true)
print_r function will output the keys. even if you use array_values the array still have indexes as keys.
Just output the the array manually using echo and implode (implode will join array values into a single string using the first parameter character):
echo implode(',', $newList);
Arrays will always have keys. If you want an array, you can get all the values, turn them into one comma separated string, and place that into an array:
$productIds = [implode(',', array_column($myList, 'product_id'))];
var_dump($productIds);
// RESULT:
// array (size=1)
// 0 => string '8085,8087,8086,8042' (length=19)

Converting multidimentional array into single array and removing the keys [duplicate]

This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 5 months ago.
array (size=1551884)
0 =>
array (size=1)
'entity_id' => string '131813' (length=6)
1 =>
array (size=1)
'entity_id' => string '213808' (length=6)
2 =>
array (size=1)
'entity_id' => string '712885' (length=6)
is it possible to convert it to single array without the key 'entity_id' without a loop?
array
0 =>
131813
1 =>
213808
2 =>
712885
I have tried this one :
call_user_func_array('array_merge', $array)
but somehow is only returning 1 element
UPDATE:
here are the benchmark results from the given answers to this question:
php version > 5.6
array_column: 0.20802903175354
foreach: 0.46231913566589
array_map: 1.021989107132
php version > 7
array_column: 0.079965829849243
foreach: 0.15323305130005
array_map: 0.28970503807068
This is also possible with array_column.
$result = array_column($your_array, 'entity_id');
You can do this very easily with array_map like this:
$result = array_map(function($value) {
return $value['entity_id'];
}, $originalArray);
Working example: https://3v4l.org/JOEMI
Of course you could also do it with a foreach loop:
$result = [];
foreach($originalArray AS $entity) {
$result[] = $entity['entity_id'];
}
Working example: https://3v4l.org/9J5XH
I prefer the first option personally.
Update: the accepted answer is clearly the best way. Do that! Leaving this here for comparison.

How to convert array into JSON object in php [duplicate]

This question already has answers here:
Converting MySQL result array to JSON [duplicate]
(2 answers)
Closed 6 years ago.
I want to convert my array value:
Array ( [page_1] => fifth [page_2] => first [page_3] => fourth [page_4] => third )
Into JSON format is given below
{s:6:"page_1";s:5:"third";s:6:"page_2";s:5:"first";s:6:"page_3";s:6:"fourth";s:6:"page_4";s:5:"fifth";}
Can anyone please help me
You want to serialize you array.
You need to use serialize()
<?php
$a = array (
'page_1' => 'fifth',
'page_2' => 'first',
'page_3' => 'fourth',
'page_4' => 'third');
echo serialize($a);
// Outputs: a:4:{s:6:"page_1";s:5:"fifth";s:6:"page_2";s:5:"first";s:6:"page_3";s:6:"fourth";s:6:"page_4";s:5:"third";}
?>
$json = json_encode($array);
and otherwise
$array = json_decode($json, true);
When I insert the value in table it is inserting like
s:107:"a:4:{s:6:"page_1";s:5:"third";s:6:"page_2";s:5:"first";s:6:"page_3";s:6:"fourth";s:6:"page_4";s:5:"fifth";}";
don't know why cause when i display it that is right but in table it is inserting something like above

Categories