Multiple arrays with same indexs in php - php

Hi I want to save records from array in php.I get arrays like
Array
(
[0] => Make
[1] => Model
[2] => Year
[3] => SKU
)
Array
(
[0] => HTC
[1] => Diamond
[2] => 2008
[3] => HTC Touch Diamond
)
Array
(
[0] => Samsung
[1] => M-900
[2] => 2007
[3] => MM-A900M
)
Array
(
[0] => AT&T
[1] => PDA
[2] => 2002
[3] => 8525PDA
)
Array
(
[0] => AT&T
[1] => PDA
[2] => 2003
[3] => 8525PDA
).
The above data coming from foreach loop like foreach($data as $row){ print_r($row);}.Now i want to save lower records against first array indexs like make,model,year and sku.The array index( make,model,year,sku) can be less or more means dynamic.How can i do that ? Thnaks

Try this :
$cnt = 0;
$res = array();
foreach($data as $row){
if($cnt ==0){
$key = array();
$key = $row;
$cnt++;
}
else{
$res[] = array_combine($key,$row);
}
}
echo "<pre>";
print_r($res);

Use build-in function array_combine

Related

Inserting calculated values during a for each loop

I have an array that looks like this.
Array
(
[0] => Array
(
[0] => 1
[1] => 500
[2] => 800
)
[1] => Array
(
[0] => 1
[1] => 100
[2] => 200
)
[2] => Array
(
[0] => 1
[1] => 300
[2] => 400
)
)
I want to use a foreach loop (not a function) to do several things. First I just want to subtract subelement [1] from subelement [2] to generate sub element [3] and then insert subelement3 back into the output array. See below:
$output=
Array
(
[0] => Array
(
[0] => 1
[1] => 500
[2] => 800
[3] => 300
)
[1] => Array
(
[0] => 1
[1] => 100
[2] => 200
[3] => 100
)
[2] => Array
(
[0] => 1
[1] => 400
[2] => 250
[3] => 150
)
)
To do the above I am using the for each loop below to generate the values of subelement 3 and it works ok. My problem is inserting the values back into the output array in the right place. I have commented out my last attempt which failed. Sorry if I am missing the obvious here.
foreach($result as $sub)
{
//get values
$sub[3]=$sub[1]-$sub[2];
echo "<difference>".$sub[3]."<br>";//works ok
//insert values back into array
//$result[$sub[0]][3] = $sub[3];
}
print "<pre>";
print_r($result);
print "</pre>";
die();
The problem:
$result[$sub[0]][3] = $sub[3]; //= $result[1][3] = $sub[3]
$sub[0] always return 1 (according to your input array) so it's not what you're looking for.
The solution:
Use the $array as $key => $val format in your foreach loop
and then access the sub array according to the relevant key.
foreach($result as $key => $sub)
{
//get values
$result[$key][3]=$sub[1]-$sub[2];
You can use array_map too, it's more elegant:
$output = aray_map( function ($item) {
$item[] = $sub[2] - $sub[1];
return $item;
}, $arr);
Demo

How to retrieve data from multiple arrays

I have two arrays that was converted from csv file.
The first csv line looks like this:
franchise_id,franchise_name,phone,website,email,region_codes;
1,"Abbott, Hackett and O`Conner",1-648-177-9510,auto-service.co/bw-319-x,Lupe-2485#auto-service.co,"36101,36055,36071";
The second csv line looks like this:
postal_code,region_code,city,state,region;
14410,36055,Adams Basin,NY,Monroe;
I converted these lines to arrays like this:
//Region Array
$region_lines = explode(PHP_EOL, $region_mappings_string);
$region_array = array();
foreach ($region_lines as $region_line) {
$region_array[] = str_getcsv($region_line);
}
//Franchise Array
$franchise_lines = explode(PHP_EOL, $franchises_string);
$franchise_array = array();
foreach ($franchise_lines as $franchise_line) {
$franchise_array[] = str_getcsv($franchise_line);
}
After that I got the result like this for Region:
Array
(
[0] => Array
(
[0] => postal_code
[1] => region_code
[2] => city
[3] => state
[4] => region;
)
[111] => Array
(
[0] => 14410
[1] => 36055
[2] => Adams Basin
[3] => NY
[4] => Monroe;
)
[112] => Array
(
[0] => 14617
[1] => 36055
[2] => Rochester
[3] => NY
[4] => Monroe;
)
And for franchise:
Array
(
[0] => Array
(
[0] => franchise_id
[1] => franchise_name
[2] => phone
[3] => website
[4] => email
[5] => region_codes;
)
[1] => Array
(
[0] => 1
[1] => Abbott, Hackett and O`Conner
[2] => 1-648-177-9510
[3] => auto-service.co/bw-319-x
[4] => Lupe-2485#auto-service.co
[5] => 36101,36055,36071;
)
What I need to do is to look for 14410 postal code from the first array, get the region_code and look for this region_code in second array and then output the results in PHP. How can this be done?
Will have to loop over franchises array and run simple comparison
function getDataByRegionCode($code, $franchiseArray) {
foreach ($franchiseArray as $key => $data) {
if(isset($data[5])){
$codes = explode(',',$data[5]);
if(in_array($code,$codes)) return $data;
}
}
return NULL;
}
print_r(getDataByRegionCode(14410,$franchiseArray));

Sorting php array by column in ascending order

Array
(
[content_type] => Array
(
[0] => story
[1] => delhi
[2] => tez
)
[type] => Array
(
[0] => video_id
[1] => subcategory
[2] => story_id
)
[fetch_id] => Array
(
[0] => 32
[1] => 32
[2] => 2
)
[order] => Array
(
[0] => 6
[1] => 4
[2] => 5
)
[label] => Array
(
[0] => dsfs fdsf dsf sdf
[1] => dfsdfs
[2] => sdfsdfsd
)
[link] => Array
(
[0] => fsd fsdf sdf
[1] => fsdfsdfdsf
[2] => fsdfdsfds
)
[record] => Array
(
[0] => 10
[1] => 8
[2] => 12
)
)
Above is the array I have to sort this array in the basis of order field and it should shorted all the fields accordingly like below example.
$arr['order'][0] = 4;
$arr['order'][1] = 5;
$arr['order'][2] = 6;
$arr['type'][0] = 'subcategory';
$arr['type'][1] = 'story_id';
$arr['type'][2] = 'video_id';
and so on.....
You can try this -
$new = array();
// Extract and get the keys as values
$order = array_flip($array['order']);
// sort them according to keys
ksort($order);
// loop through main array
foreach($array as $key => $sub_array) {
// loop through order
foreach ($order as $o) {
// store the new value according to order
$new[$key][] = $sub_array[$o];
}
}
Demo
Some lesser solution:
asort($array['order']);
foreach ($array as $key => $subArray) {
$array[$key] = array_replace($array['order'], $subArray);
}
For reset a key sequence you may just to use array_values().

Group values from array

I have this array:
SimpleXMLElement Object
(
[id] => Array
(
[0] => Koala.jpg
[1] => Jellyfish.jpg
)
[desc] => Array
(
[0] => koaladesc
[1] => jelly desc
)
[qtidade] => Array
(
[0] => 1
[1] => 5
)
I need create some php function that help me group the values like this:
[0] => Array
(
[0] => Koala.jpg
[1] => koaladesc
[2] => 1
)
[1] => Array
(
[0] => Jellyfish
[1] => jelly desc
[2] => 5
)
Could anyone help me?
Something like this should do the trick, but it's localized to what you're asking based on the vagueness of your question:
$new_array = array();
foreach($simple_xml_object as $obj) {
if(is_array($obj)) {
for($i = 0; $i < count($obj); $i++) {
$new_array[$i][] = $obj[$i];
}
}
}
I would suggest looking at the documentation on the foreach() construct, as well as looking over the SimpleXML manual.
So, you want to tranpose an array. Here's a magical way of transposing rectangular arrays:
array_unshift($array, null);
$array = call_user_func_array('array_map', $array);
let's suppose your array is saved in variable $arrayValues
$arrayValues = [id] => Array
(
[0] => Koala.jpg
[1] => Jellyfish.jpg
)
[desc] => Array
(
[0] => koaladesc
[1] => jelly desc
)
[qtidade] => Array
(
[0] => 1
[1] => 5
)
now you need to create the following code:
foreach($arrayValues as $array)
{
echo $array->id;
echo $array->desc;
echo $array->qtidade;
}
this may work well for you.

Randomize the data in a multidimensional array (second tier)

I have a multidimensional array like this one:
Array
(
[site1] => Array
(
[0] => Array
(
[0] => data
[1] => data
[2] => data
[3] => data
)
[1] => Array
(
[0] => data
[1] => data
[2] => data
[3] => data
)
[2] => Array
(
[0] => data
[1] => data
[2] => data
[3] => data
)
[site2] => Array
(
[0] => Array
(
[0] => data
[1] => data
[2] => data
[3] => data
)
[1] => Array
(
[0] => data
[1] => data
[2] => data
[3] => data
)
[2] => Array
(
[0] => data
[1] => data
[2] => data
[3] => data
)
)
)
I am trying to randomize the data for each site ([site1], [site2]) but don't mix the data between sites. So it would be like a second tire randomization.
For example, after the randomization, the position [0] for [site1] would have different data, maybe the data from the earlier position [3].
Any ideas how to do it?
You map the shuffle function to the array:
$shuffle = function($array) {
$r = shuffle($array);
return $array;
};
$sites = array_map($shuffle, $sites);
Or:
foreach ($sites as &$site) shuffle($site);
unset($site);
foreach($sites as $i => $site) {
shuffle($sites[$i]);
}
Another, (not as good as shuffle) way ;-)
foreach ($site as $key => $data) {
$values = array();
$rand = array_rand($data, count($data));
for ($i = 0; $i < count($rand); $i++)) {
$values[] = $site[$key][$rand[$i]];
}
$site[$key] = $values;
}

Categories