Split array in php - php

I am getting result in php. I want to split the array to get each value in variable or list. The result is like this:
Array
(
[success] => 1
[result] => Array
(
[id] => 12345678ABCDEXXX
[userid] => 2950
[system_user_id] => 76
[coin] => TRX
[amount] => 11.110000
[feePercent] => 0
[feeAmount] => 0
[memoTag] =>
[networkFee] => 0
[address] => TESTADDRESSHERE
[confirmations] => 0
[callBackUrl] => www.xyz.com/test
[transactionStatus] => WaitingForTransaction
[transactionType] => Deposit
[createdOn] => 1643692929
[modifiedOn] => 1643692929
[expiredon] => 1644988929
[description] => This is description
)
)
how to split and get every value?

This may answer your question
https://www.php.net/manual/en/function.extract.php
extract($array)

Php Multidimensional array to variable
<?php
$array = Array
(
'success' => '1',
'result' => array(
'id' => '12345678ABCDEXXX',
'userid' => '2950',
'system_user_id' => '76',
'coin' => 'TRX',
'amount' => '11.110000',
'feePercent' => '0',
'feeAmount' => '0',
'memoTag' => '',
'networkFee' => '0',
'address' => 'TESTADDRESSHERE',
'confirmations' => '0',
'callBackUrl' => 'www.xyz.com/test',
'transactionStatus' => 'WaitingForTransaction',
'transactionType' => 'Deposit',
'createdOn' => '1643692929',
'modifiedOn' => '1643692929',
'expiredon' => '1644988929',
'description' => 'This is description'
)
);
function extractMultidimentional($array){
foreach ($array as $key => $value) {
if(is_array($value)){
extractMultidimentional($value);
}else{
global ${$key};//Set as global
${$key} = $value;//Assign with its value
}
}
}
extractMultidimentional($array);
// Now you can call each key as a variable
echo $success;//1
echo $system_user_id;//76
echo $expiredon;//1644988929
If you have multidimensional array and those have same key then the variable will store last array value

You can use array_chunk like this
$array = array('value -1', 'value 2', 'value 3', 'value 4', 'value 5','value -6','value -7');
$newArrays = array_chunk($array,2); // apply array chunk
echo "";
// print the first segment (position) array after splitting that array.
print_r($newArrays[0]);

Related

Remove some keys from an array inside another array in PHP (Laravel)

How to remove some keys from an array inside another array in PHP?
I have this structure:
array (
0 =>
array (
'num' => '123',
'nome' => 'test 001'
'pontos' => 68,
'data_status' => '03/09/2021 10:05',
'uuid_status' => '69450ea451ae11ea85ca309c23d3a0ed'
),
1 =>
array (
'num' => '345',
'nome' => 'test 002'
'pontos' => 120,
'data_status' => '27/08/2021 15:46',
'uuid_status' => '3cbf4fd15d5411ea86956eef5d66cb13',
),
)
and need to return something like this:
array (
0 =>
array (
'num' => '123',
'nome' => 'test 001'
'pontos' => 68
),
1 =>
array (
'num' => '345',
'nome' => 'test 002'
'pontos' => 120
)
)
I've seen some answers but they seem to be outdated, also i'm using Laravel, so it would help if someone point me out something from the framework and its equivalent in pure PHP
A simple foreach loop is a good starting point, note this will remove items from the original array, by using the reference & to the array rather than a normal simple copy. On the $val in the foreach ( $input as &$val){
$input = [
[
'num' => '123',
'nome' => 'test 001',
'pontos' => 68,
'data_status' => '03/09/2021 10:05',
'uuid_status' => '69450ea451ae11ea85ca309c23d3a0ed'
],[
'num' => '345',
'nome' => 'test 002',
'pontos' => 120,
'data_status' => '27/08/2021 15:46',
'uuid_status' => '3cbf4fd15d5411ea86956eef5d66cb13'
]
];
$remove = ['data_status','uuid_status'];
foreach ( $input as &$val){
foreach ($val as $k => $v) {
if ( in_array($k,$remove) ) {
unset( $val[$k]);
}
}
}
print_r($input);
RESULT
Array
(
[0] => Array
(
[num] => 123
[nome] => test 001
[pontos] => 68
)
[1] => Array
(
[num] => 345
[nome] => test 002
[pontos] => 120
)
)
Probably not the most concise, but it gets the job done:
$bad_keys = array('data_status', 'uuid_status');
$array = array (
0 =>
array (
'num' => '123',
'nome' => 'test 001',
'pontos' => 68,
'data_status' => '03/09/2021 10:05',
'uuid_status' => '69450ea451ae11ea85ca309c23d3a0ed',
),
1 =>
array (
'num' => '345',
'nome' => 'test 002',
'pontos' => 120,
'data_status' => '27/08/2021 15:46',
'uuid_status' => '3cbf4fd15d5411ea86956eef5d66cb13',
),
);
function traverse_array($array, $bad_keys) {
foreach ($array as $key => $value) {
if (is_array($value)) {
$array[$key] = traverse_array($value, $bad_keys);
} else {
foreach ($bad_keys as $remove_me) {
if ($key == $remove_me) {
unset($array[$key]);
}
}
}
}
return $array;
}
print_r(traverse_array($array, $bad_keys));

Create data set in php (loop)

Hello i am trying to create data set like
Expected output:-
Array
(
[0] => Array
(
[sku] => sku
[variant_option_one_name] => Color
[variant_option_one_value] => Cyan
),
[1] => Array
(
[sku] => sku
[variant_option_one_name] => Color
[variant_option_one_value] => Red
)
)
but i am not sure what is missing in code.
Here is the code
$array = array(
0 => array(
'id_product_attribute' => '17615',
'id_product' => '2295',
'reference' => '',
'available_date' => '0000-00-00',
'vend_id' => null,
'id_shop' => '1',
'id_attribute_group' => '1',
'is_color_group' => '1',
'group_name' => 'Color',
'attribute_name' => 'Cyan',
'id_attribute' => '1',
),
1 => array(
'id_product_attribute' => '17616',
'id_product' => '2295',
'reference' => '',
'available_date' => '0000-00-00',
'vend_id' => null,
'id_shop' => '1',
'id_attribute_group' => '1',
'is_color_group' => '1',
'group_name' => 'Color',
'attribute_name' => 'Red',
'id_attribute' => '21',
),
);
$ids = array();
foreach ($array as $combinations) {
$ids['sku'] = 'sku';
$ids['variant_option_one_name'] = $combinations['group_name'];
$ids['variant_option_one_value'] = $combinations['attribute_name'];
}
print_r($ids);//
Here i am getting
Array
(
[sku] => sku
[variant_option_one_name] => Color
[variant_option_one_value] => Red
)
The above output i am getting. Seems like data is overwrite
Any correction to get both the data ?
I do not get both the colors in array. It
Thankyou
You're absolutely right, the values are being overwritten each time.
What you need to do is, each time you loop you should create a new array containing your values, and then assign that array to a new index inside the main array (so you get an array of arrays, like the expected output you've shown):
foreach ($array as $combinations) {
$arr = array();
$arr['sku'] = 'sku';
$arr['variant_option_one_name'] = $combinations['group_name'];
$arr['variant_option_one_value'] = $combinations['attribute_name'];
$ids[] = $arr;
}
Live Demo: http://sandbox.onlinephpfunctions.com/code/3a0cf7f8cbb994ef4192c1e23493bef397785937
foreach ($array as $combinations) {
array_push ($ids, [
'sku' => 'sku',
'variant_option_one_name' => $combinations['group_name'],
'variant_option_one_value' => $combinations['attribute_name']
]);
}

Loop through php array content returning content to screen

I have an array that look like this($myArray)
Array ( [0] => Array ( [0] => Array ( [ID] => 322 [Number] => 1 [Date] => 3117-01-41 [example] => Hello )
[1] => Array ( [ID] => 123 [Number] => 49 [Date] => 1717-05-21 [example] => Hi )
[2] => Array ( [ID] => 007A [Number] => 42 [Date] => 2005-11-24 [example] => Some Text )
[3] => Array ( [ID] => 999AAA [Number] => 492 [Date] => 3117-01-21 [example] => Text Test Text )))
In my page i am using a function which returns content($content) and that content is displayed onto the web browser. No echoing or printing just returning the content variable which is constantly being appended to.
I deally i want to loop through my array and print the values from a certain field to the screen for example
while(//not sure what goes here){
$content .= '<p>'.$someVariable["Number"].'</p>';
$content .= '<p>'.$someVariable["example"].'</p>';
$content .= '<p>'.$someVariable["date"].'</p>';
}
Im not sure if the while loop is the best way to achieve the desired result. Also using fetch_array is not an option i can use because it breaks previous code.
<?php
$yourArray = array (
array (
'ID' => 322,
'Number' => 1,
'Date' => '3117-01-41',
'example' => 'Hello'
),
array (
'ID' => 123,
'Number' => 49,
'Date' => '1717-05-21',
'example' => 'Hi'
),
array (
'ID' => '007A',
'Number' => 42,
'Date' => '2005-11-24',
'example' => 'Some Text'
),
array (
'ID' => '999AAA',
'Number' => 492,
'Date' => '3117-01-21',
'example' => 'Text Test Text'
)
);
?>
print whole array:
<?php print_r($yourArray); ?>
print certain levels of array:
<?php print_r($yourArray[2]); ?>
print certain element of array:
print_r($yourArray[2]['Date']);
print array level by level:
<?php
for($index=0; $index < count($yourArray); $index++){
echo $yourArray[$index]['ID'].'<br>';
echo $yourArray[$index]['Number'].'<br>';
echo $yourArray[$index]['Date'].'<br>';
echo $yourArray[$index]['example'].'<br>';
};
?>
This helps?
<?php
$yourArray = array (
array (
'ID' => 322,
'Number' => 1,
'Date' => '3117-01-41',
'example' => 'Hello'
),
array (
'ID' => 123,
'Address' => '1283 street, 576',
'Date' => '1717-05-21',
'country' => 'Canada'
),
array (
'ID' => '007A',
'Number' => 42,
'Date' => '2005-11-24',
'example' => 'Some Text'
),
array (
'ID' => '999AAA',
'Number' => 492,
'Date' => '3117-01-21',
'example' => array(
'subID' => 45,
'subAlias' => 'teste sub item'
)
)
);
$output = "";
function walkerFunction($item, $key) {
global $output;
$output .= $key . "->" . $item . PHP_EOL;
}
array_walk_recursive($yourArray, "walkerFunction");
echo $output;
It seems you are seeking this:
$yourArray = array (
array (
'ID' => 322,
'Number' => 1,
'Date' => '3117-01-41',
'example' => 'Hello'
),
array (
'ID' => 123,
'Number' => 49,
'Date' => '1717-05-21',
'example' => 'Hi'
),
array (
'ID' => '007A',
'Number' => 42,
'Date' => '2005-11-24',
'example' => 'Some Text'
),
array (
'ID' => '999AAA',
'Number' => 492,
'Date' => '3117-01-21',
'example' => 'Text Test Text'
)
);
$content = '';
foreach($yourArray as $key => $value):
$content .= '<p>'.$value["Number"].'</p>';
$content .= '<p>'.$value["example"].'</p>';
$content .= '<p>'.$value["Date"].'</p>';
endforeach;
print $content;

PHP Array Search and Delete

I have two array:
Array 1:
Array ( [0] => Array ( [id] => et1 [supplier_id] => 4 [supplier_product_code] => 00054X [is_active] => 1 )
[1] => Array ( [id] => et2 [supplier_id] => 4 [supplier_product_code] => 000558 [is_active] => 1 )
[2] => Array ( [id] => et3 [supplier_id] => 5 [supplier_product_code] => 00054X [is_active] => 1 ));
Array 2:
Array ( [0] => Array ([id] => et1 [same_sku] => et3);
I need to delete all the same_skus in array1 from array2.
So from my result array I need array1 to be:
Array ( [0] => Array ( [id] => et1 [supplier_id] => 4 [supplier_product_code] => 00054X [is_active] => 1 )
[1] => Array ( [id] => et2 [supplier_id] => 4 [supplier_product_code] => 000558 [is_active] => 1 ));
Code that I have right now does not work.
public function search_array($array, $val)
{
foreach ($array as $key => $row)
{
if ($row['id'] === $val)
{
return $key;
}
}
}
foreach($array2->result() as $row)
{
$id = $row->id;
$same_sku = $row->same_sku;
$key = $this->search_array($array1, $id);
if(!empty($key))
{
$same_sku_key = $this->search_array($array1, $same_sku);
if(!empty($same_sku_key))
unset($array1[$same_sku_key]);
}
}
In the following code I have recreated the two arrays from your example. I then created a function that removes from a haystack array (array1) all of the sub arrays that have an "id" that matches the value of "same_sku" within a needle array (array2). The final line echos the result array.
EDIT
I have modified the original answer to pass the array values by reference and unset the unwanted sub arrays, instead of passing by value, looping, and returning another array. This should resolve the memory issue, as well as the other issue mentioned in your comment.
$array1 = array(
array(
'id' => 'et1',
'supplier_id' => '4',
'supplier_product_code' => '00054X',
'is_active' => '1'
),
array(
'id' => 'et2',
'supplier_id' => '4',
'supplier_product_code' => '000558',
'is_active' => '1'
),
array(
'id' => 'et3',
'supplier_id' => '5',
'supplier_product_code' => '00054X',
'is_active' => '1'
),
array(
'id' => 'et4',
'supplier_id' => '5',
'supplier_product_code' => '00054X',
'is_active' => '1'
)
);
$array2 = array(
array(
'id' => 'et1',
'same_sku' => 'et3'
),
array(
'id' => 'et2',
'same_sku' => 'et4'
)
);
function remove_same_sku(&$haystack, &$needles){
foreach($needles as $needle){
foreach($haystack as $key => $val){
if($val['id'] === $needle['same_sku']){
unset($haystack[$key]);
}
}
}
}
remove_same_sku($array1, $array2);
echo print_r($array1);

Loop PHP array based in first array

I just found out how to loop an array based on another array, but my problem is that if the second array is just 1 object than it works fine but I want to make it work with two objects.
Here is an example how it do works,
$shorten = array(
0 => 'ECAR',
1 => 'CCAR',
2 => 'ICAR',
3 => 'SCAR',
4 => 'FCAR',
5 => 'PCAR',
);
$data = array(
'Hertz' => array(
'ECAR' => '49.21',
'CCAR' => '71.04',
'ICAR' => '89.58',
'SCAR' => '100.00',
)
),
'Avis' => array(
'ECAR' => '412.00',
'CCAR' => '347.00',
'ICAR' => '285.00',
'SCAR' => '224.00',
'FCAR' => '165.00',
'PCAR' => '100.00',
)
),
);
// default array as the base
$shorten = array_combine($shorten, array_fill(0, count($shorten), 'n/a'));
foreach($data as &$array) {
// merge to get set members
$array = array_merge($shorten, $array);
}
unset($array);
print_r($data);
But I want to make it work with this one,
$shorten = array(
0 => 'ECAR',
1 => 'CCAR',
2 => 'ICAR',
3 => 'SCAR',
4 => 'FCAR',
5 => 'PCAR',
);
$data = array(
'Hertz' => array(
'NYCT01' => array(
'ECAR' => '49.21',
'CCAR' => '71.04',
'ICAR' => '89.58',
'SCAR' => '100.00',
)
),
'Avis' => array(
'NYCT01' => array(
'ECAR' => '412.00',
'CCAR' => '347.00',
'ICAR' => '285.00',
'SCAR' => '224.00',
'FCAR' => '165.00',
'PCAR' => '100.00',
)
),
);
// default array as the base
$shorten = array_combine($shorten, array_fill(0, count($shorten), 'n/a'));
foreach($data as $firstArray) {
foreach($firstArray as &$array){
// merge to get set members
$array = array_merge($shorten, $array);
}
}
unset($array);
print_r($data);
And this is what I want it to be,
Array
(
[Hertz] => Array
(
[ECAR] => 49.21
[CCAR] => 71.04
[ICAR] => 89.58
[SCAR] => 100.00
[FCAR] => n/a
[PCAR] => n/a
)
[Avis] => Array
(
[ECAR] => 412.00
[CCAR] => 347.00
[ICAR] => 285.00
[SCAR] => 224.00
[FCAR] => 165.00
[PCAR] => 100.00
)
)
foreach($data AS $company => $nyc){
$inner = $nyc['NYCT01'];
foreach($shorten AS $car){
if(array_key_exists($car, $inner)){
$output[$company][$car] = $inner[$car];
}else {
$output[$company][$car] = "n/a";
}
}
}
Just loop through the data and check if the key of any of the cars exists inside the NYCT01 array, if it does set the value, else, "n/a".
My output:
Array
(
[Hertz] => Array
(
[ECAR] => 49.21
[CCAR] => 71.04
[ICAR] => 89.58
[SCAR] => 100.00
[FCAR] => n/a
[PCAR] => n/a
)
[Avis] => Array
(
[ECAR] => 412.00
[CCAR] => 347.00
[ICAR] => 285.00
[SCAR] => 224.00
[FCAR] => 165.00
[PCAR] => 100.00
)
)
Ok, got it work this is the real code I figured,
foreach($data as $company => $nyc){
foreach($nyc as $inner => $s){
foreach($shorten as $car){
if(array_key_exists($car, $nyc[$inner])){
$output[$company][$car] = $nyc[$inner][$car];
}else {
$output[$company][$car] = "n/a";
}
}
}
}
Thanks so much for your help Marcus,
Good luck.

Categories