splite value array ini php - php

i really need help about arrays. I have a case like this
[
{"animal":"lion", "car":"honda"},
{"animal":"rabbit","car":"BMW"},
{"animal":"rat", "car":"Toyota"},
{"animal":"mouse", "car":"Suzuki"},
{"animal":"horse", "car":"mercedes"},
{"animal":"dog", "car":"Jaguar"},
{"animal":"cat", "car":"Audi"}
]
how in php code it can split become 2 category like this
$animal = [ "lion","rabbit","rat","mouse","horse","dog","cat"];
and
$car = ["honda","BMW","Toyota","Suzuki","mercedes","Jaguar","Audi"];
I really need to solve this. please help me

Json_decode the string and use array_column to get the columns in each variable.
$json = '[
{"animal":"lion", "car":"honda"},
{"animal":"rabbit","car":"BMW"},
{"animal":"rat", "car":"Toyota"},
{"animal":"mouse", "car":"Suzuki"},
{"animal":"horse", "car":"mercedes"},
{"animal":"dog", "car":"Jaguar"},
{"animal":"cat", "car":"Audi"}
]';
$arr = json_decode($json,1);
$animals = array_column($arr, "animal");
$cars = array_column($arr, "car");
var_dump($cars, $animals);
https://3v4l.org/NtL9e

Real easy. Decode the json, create two blank arrays, loop through, and add to each array:
<?php
$x = '[
{"animal":"lion", "car":"honda"},
{"animal":"rabbit","car":"BMW"},
{"animal":"rat", "car":"Toyota"},
{"animal":"mouse", "car":"Suzuki"},
{"animal":"horse", "car":"mercedes"},
{"animal":"dog", "car":"Jaguar"},
{"animal":"cat", "car":"Audi"}
]';
$y = json_decode($x, true);
$animals = [];
$cars = [];
foreach ($y as $z) {
$animals[] = $z['animal'];
$cars[] = $z['car'];
}
var_dump($animals);
var_dump($cars);
https://3v4l.org/Fb7Bq

try that:
$arr=json_decode('[
{"animal":"lion", "car":"honda"},
{"animal":"rabbit","car":"BMW"},
{"animal":"rat", "car":"Toyota"},
{"animal":"mouse", "car":"Suzuki"},
{"animal":"horse", "car":"mercedes"},
{"animal":"dog", "car":"Jaguar"},
{"animal":"cat", "car":"Audi"}
]');
var_dump($arr);echo'<br>';echo'<br>';
$animal=array();
$car=array();
foreach ($arr as $row){
$animal[]=$row->animal;
$car[]=$row->car;
}
var_dump($car);
var_dump($animal);

Related

Get first number in array list

I use laravel. And then when from submitted, it pass value as array.
My question is, how i can split the value? I want number before '-' and after '-'.
I want just like below:
and
My code just like below
$form = $request->all();
$emelto = $form['emelto'];
$split = explode('-', $emelto );
but it shows error
explode() expects parameter 2 to be string, array given
Please someone can help me.
Explode each individual string as you tried.
Insert each number in it's respective column index in result.
Snippet:
<?php
$emelto = [
'4-2',
'11-5'
];
$data = [];
foreach($emelto as $str){
foreach(explode('-',$str) as $index => $s){
$data[$index] = $data[$index] ?? [];// assuming your PHP version supports ??
$data[$index][] = $s;
}
}
print_r($data);
You can use
$array1 = array();
$array2 = array();
foreach($request->emelto as $val){
$dataArray = explode('-', $val);
$array1[] = $dataArray[0];
$array2[] = $dataArray[1];
}
$emelto is an array and you are passing it to explode. You have to pass it like $emelto[0].
You should update your code with the below one.
<?php
$form = $request->all();
$emelto = $form['emelto'];
$array1 = explode('-', $emelto[0] );
$array2 = explode('-', $emelto[1] );
$array3 = array();
array_push($array3, $array1[0]);
array_push($array3, $array2[0]);
$array4 = array();
array_push($array4, $array1[1]);
array_push($array4, $array2[2]);
?>

Get last item URL

I have a data in following JSON sort of format in which I am trying to only get the very last URL which in my following example is https://lastexample.com/images/I/4162nAIaRCL.jpg . Is there an easy way I can retrieve this via PHP?
{"https://example.com/images/I/4162nAIaRCL._SX425_.jpg":[425,425],
"https://example.com/images/I/4162nAIaRCL._SY355_.jpg":[355,355],
"https://example.com/images/I/4162nAIaRCL._SX466_.jpg":[466,466],
"https://example.com/images/I/4162nAIaRCL._SY450_.jpg":[450,450],
"https://lastexample.com/images/I/4162nAIaRCL.jpg":[500,500]}
Try, decode it into an array (json_decode) then get the keys (array_keys), then the last entry (end/max).
<?php
$json = '{
"https://example.com/images/I/4162nAIaRCL._SX425_.jpg": [425, 425],
"https://example.com/images/I/4162nAIaRCL._SY355_.jpg": [355, 355],
"https://example.com/images/I/4162nAIaRCL._SX466_.jpg": [466, 466],
"https://example.com/images/I/4162nAIaRCL._SY450_.jpg": [450, 450],
"https://lastexample.com/images/I/4162nAIaRCL.jpg": [500, 500]
}';
$array = json_decode($json, true);
$keys = array_keys($array);
echo end($keys); // https://lastexample.com/images/I/4162nAIaRCL.jpg
https://3v4l.org/MLbdt
Try this :
<?php
$json = '{
"https://example.com/images/I/4162nAIaRCL._SX425_.jpg":[425,425],
"https://example.com/images/I/4162nAIaRCL._SY355_.jpg":[355,355],
"https://example.com/images/I/4162nAIaRCL._SX466_.jpg":[466,466],
"https://example.com/images/I/4162nAIaRCL._SY450_.jpg":[450,450],
"https://lastexample.com/images/I/4162nAIaRCL.jpg":[500,500]
}';
$arr = json_decode($json,true);
$arrkeys = array_keys($arr);
$lastkey = end($arrkeys);
echo $lastkey;
?>

How to edit JSON objects

I'm on PHP and I need to edit a JSON output to return only objects >=0 and divided by one hundred
Eg.
$json = {"data":[0,55,78,-32,-46,37]}
Needed
$json = {"data":[0,0.55,0.78,0.37]}
How this can be done?
Well, I know this is not the best practice, but if it's as simple as this, you can do the following.
$json = '{"data":[0,55,78,-32,-46,37]}';
// decoding the string to objects & arrays
$x = json_decode($json);
// applying a function on each value of the array
$x->data = array_map(
function($a)
{
if( $a >= 0 ) return $a/100;
else return null;
},
$x->data
);
// Removing empty values of the array
$x->data = array_filter($x->data);
// making a JSON array
$jsonData = json_encode(array_values($x->data));
// inserting a JSON array in a JSON Object
$json = '{"data":' . $jsonData . '}';
// here is your {"data":[0,0.55,0.78,0.37]}
echo $json;
Hope it helps !
Btw, I had to trick the json encode with array_values to prevent the creation of an object rather than an array for the data content. But I guess there is a better method that I just don't know ...
EDIT :
Find out the way :D
Once empty values removed from the array, just do :
$x->data = array_values($x->data);
$json = json_encode($x);
This will do the trick and it will not create issues with the rest of the object.
Alessandro:
Here's my approach, feel free to try. json_decode and a simple foreach can help you...
Code:
$json = array();
$result = array();
$json = '{"data":[0,55,78,-32,-46,37]}';
$decoded_json=json_decode($json, TRUE);
foreach ($decoded_json['data'] as &$value) {
if ($value >= 0){
$value = $value / 100;
$result[]=$value;
}
}
echo json_encode($result);
?>
Result:
[0,
0.55,
0.78,
0.37
]

loop and insert into another array without redundancy

How to skip inserting repeating same value of array items in my case? I know there is built in function like array unique but my case is different.
Below code produced 12345555555667 but I just want 1234567, means not a value is duplicate.
$NumArray = array('1','2','3','4','5','5','5','5','5','5','5','6','6','7');
$SecondArray = array();
foreach($NumArray as $num){
$SecondArray[] = array(
'alpa'=>$num
);
}
echo json_encode($SecondArray);
If $NumArray were static, it's as simple as doing:
foreach (array_unique($NumArray) as $num)
{
//same code as before
}
But given that it isn't, you could just write:
$SecondArray = ();
foreach ($NumArray as $num)
{
if (!isset($SecondArray[$num]))
{//key does not exist (yet)
$SecondArray[$num] = array('alpa' => $num);
}
}
echo json_encode(array_values($SecondArray));//remove keys, to ensure a JSON array
see the docs
The array_values call ensures the resulting JSON string is a JS array, and not an object. For example, if $num was 1, 2, 3, the JSON string without array_values would've looked like this:
{
1: {alpha: 1},
2: {alpha: 2},
3: {alpha: 3}
}
With that call, it looks like this:
[
{alpha: 1},
{alpha: 2},
{alpha: 3}
]
Create a new array, then loop the $NumArray to check if the value already exists... if not, add it to the new array.
<?php
$NumArray = array('1','2','3','4','5','5','5','5','5','5','5','6','6','7');
$newArray = array();
foreach( $NumArray as $val ) {
if( !in_array( $val, $newArray ) ) {
$newArray[] = $val;
}
}
echo '<pre>';
print_r( $newArray );
echo '</pre>';
?>
What about:
$NumArray = array('1','2','3','4','5','5','5','5','5','5','5','6','6','7');
$SecondArray = array();
foreach($NumArray as $key => $value){
if(in_array($value, $NumArray){
array_push($SecondArray, $value);
}
}
echo json_encode($SecondArray);

Similar values in php array

I have an array that stores some values. I'm trying to detect the similar values and add them to new array.
example:
$arrayA = array( 1,4,5,6,4,2,1);
$newarray = (4,1);
Any help?
Use the array_intersect() method. For example
$arrayA = array(1,4,5,6,4,2,1);
$arrayB = array(4,1);
$common_values = array_intersect($arrayA, $arrayB);
try this:
$array = array(1,4,5,6,4,2,1);
$duplicates = array_unique(array_diff_assoc($array, array_unique($array)));
$a1 = array( 1,4,5,6,4,2,1);
$a = array();
foreach($a1 as $value){
if(!in_array($value, $a)){
$a[] = $value;
}
}
$arrayA = array(1,4,5,6,4,2,1);
$newarray = array_diff_assoc($arrayA, array_unique($arrayA));

Categories