How to map the two arrays with a duplicate value? - php

I have two arrays.
$array1 = ['id_e' =>[91707, 91708]];
$array2 = ['id_s' => [18, 57]];
If I want to insert or delete into the database, I want to make one-to-many mappings on these two arrays. And the result I expect it to be a new array as shown below.
Final Array:
array (size=4)
0 =>
array (size=2)
'id_e' => int 91707
'id_s' => int 18
1 =>
array (size=2)
'id_e' => int 91707
'id_s' => int 57
array (size=2)
2 =>
array (size=2)
'id_e' => int 91708
'id_s' => int 18
3 =>
array (size=2)
'id_e' => int 91708
'id_s' => int 57
I'm stuck after returning array1 and array2. I'm a beginner in php.
How do I do this?

Easiest way is:
$res = array();
foreach ($array1['id_e'] as $ide)
foreach ($array2['id_s'] as $ids)
$res[] = array('id_e' => $ide, 'id_s' => $ids);

Related

Adding Array of values to a multidimentional Array in PHP

I would like to add values from a $secondArray to $firstArray:
$firstArray = [
0 => [
'prodID' => 101,
'enabled' => 1,
],
1 => [
'prodID' => 105,
'enabled' => 0,
],
];
The $secondArray will always have the same amount of array items and will be in the same order as the $firstArray:
$secondArray = [34, 99];
This is what I have tried but I keep getting the wrong stockQT values after the exercise:
foreach ($secondArray as $value) {
foreach ($firstArray as &$firstArray) {
$firstArray['stockQT'] = $value;
}
}
Incorrect Result for a var_dump($firstArray):
array (size=2)
0 =>
array (size=3)
'prodID' => int 101
'subscribed' => int 1
'stockQT' => int 99
1 =>
array (size=3)
'prodID' => int 105
'subscribed' => int 0
'stockQT' => int 99
I have looked at similar posts but cant seem to get the correct vales after using different suggestions like while() loops. Below is what I need the $firstArray to look like:
array (size=2)
0 =>
array (size=3)
'prodID' => int 101
'subscribed' => int 1
'stockQT' => int 34
1 =>
array (size=3)
'prodID' => int 105
'subscribed' => int 0
'stockQT' => int 99
You just need one foreach() using the key since $secondArray will always have the same amount of array items and will be in the same order as the $firstArray. Notice the & to modify the actual value in the array:
foreach($firstArray as $key => &$value) {
$value['stockQT'] = $secondArray[$key];
}
Or alternately loop $secondArray and use the key to modify $firstArray:
foreach($secondArray as $key => $value) {
$firstArray[$key]['stockQT'] = $value;
}

Laravel 5 adding an element in an array does not work

I am getting an array from table questions
$questions = Category::where('slug', $slug)->first()->questions->toArray();
then using foreach I iterate over array and push an array element during each iteration like below
foreach ($questions as $question) {
$question['options'] = Option::where('question_id', $question['id'])->get()->toArray();
}
Ideally I should get response like below
array (size=5)
0 =>
array (size=6)
'id' => int 1
'category_id' => int 1
'questions' => string 'The ozone layer restricts' (length=25)
'answer_id' => int 4
'image_path' => string '' (length=0)
'options' =>
array (size=4)
0 =>
array (size=3)
...
1 =>
array (size=3)
...
2 =>
array (size=3)
...
3 =>
array (size=3)
...
1 =>
array (size=6)
'id' => int 2
'category_id' => int 1
'questions' => string 'Ecology deals with' (length=18)
'answer_id' => int 10
'image_path' => string '' (length=0)
'options' =>
array (size=4)
0 =>
array (size=3)
...
1 =>
array (size=3)
...
2 =>
array (size=3)
...
3 =>
array (size=3)
...
but Actually I am not getting options key into my questions array
The issue here is when you do a foreach, you are referring to the array by value - that is, under the hood PHP will create a copy of the variable inside the array. What you need is to access each element in the foreach by reference (note the & next to $q)
foreach ($questions as & $q) {
$q['options'] = Option::where('question_id', '=', $q['id'])->get()->toArray();
}
See:
Passing by references and the PHP docs on foreach.
Note: This is not really a Laravel issue, just PHP.
Try this (& in foreach):
foreach ($questions as & $question) {
$question['options'] = Option::where('question_id', $question['id'])->get()->toArray();
}
You can try:
foreach ($questions as $key=>$question) {
$questions[$key]['options'] = Option::where('question_id',$question['id'])->get()->toArray();
}

php Get all values from multidimensional associative array

how can i get all values from multidimensional associative array
I dont want to use print_r want to control my array put all the value in normal array with unique values
my array is look like this
array (size=10)
0 =>
array (size=3)
0 =>
array (size=1)
'Campaign' => string 'DEMO' (length=4)
1 =>
array (size=1)
'Campaign' => string 'Home_Sec' (length=8)
2 =>
array (size=1)
'Campaign' => string '' (length=0)
1 =>
array (size=0)
empty
2 =>
array (size=0)
empty
3 =>
array (size=1)
0 =>
array (size=1)
'Campaign' => string 'Back_Brace' (length=10)
4 =>
array (size=2)
0 =>
array (size=1)
'Campaign' => string 'Home_Sec' (length=8)
1 =>
array (size=1)
'Campaign' => string '' (length=0)
5 =>
array (size=1)
0 =>
array (size=1)
'Campaign' => string 'home_Sec_2' (length=10)
6 =>
array (size=1)
0 =>
array (size=1)
'Campaign' => string 'Burial_Ins' (length=10)
7 =>
array (size=0)
empty
8 =>
array (size=0)
empty
9 =>
array (size=0)
empty
I dont want to use print_r want to control my array put all the value in normal array with unique values
array_walk is an option, but here's another option if you want to try something a bit more coded by yourself, solving this problem recursively
This will flatten any n-max level array into a single array that contains all the values of all the sub arrays (including the initial array itself)
<?php
$array = array(
1 => array(1, 2, 3, 4 => array(
1, 2, 3, 4
)),
4, 5);
function recurse_values($array) {
if (is_array($array)) {
$output_array = array();
foreach ($array as $key=>$val) {
$primitive_output = recurse_values($val);
if (is_array($primitive_output)) {
$output_array = array_merge($output_array, $primitive_output);
}
else {
array_push($output_array, $primitive_output);
}
}
return $output_array;
}
else {
return $array;
}
}
print_r(recurse_values($array));
?>
If you need unique values, at the end you can add a array_unique to do this.
You can use array_walk
$array = array(...); //your values here
function output($item, $key) {
echo $key . ' =>' . $item;
}
array_walk($array, 'output');
Are you asking how you can "flatten" this multi-dimensional array into a one dimension? Possible solutions to similar problems... How to Flatten a Multidimensional Array?

Php Array from database not getting desired array stucture

I have problem with building multi-dimensional array by pulling data from database.
I have a list of Sales Representative (sr) in my database. I run query and selected all sr.
Now I have to create array like this:
array (size=3)
'Manoj' =>
array (size=3)
'mc_count' => 0
'auto_count' => 0
'in_count' => 0
'Bharat' =>
array (size=3)
'mc_count' => 0
'auto_count' => 0
'in_count' => 0
'Pradeep' =>
array (size=3)
'mc_count' => 0
'auto_count' => 0
'in_count' => 0
To create this I have written following code:
<?php
$sr_array=array();
$sr_sql= "select DISTINCT sr from sales_invoice";
$sr_query = mysqli_query($connection, $sr_sql);
while($sr_result = mysqli_fetch_assoc($sr_query)){
$sr_array []= array($sr_result["sr"]=>array(“mc_count”,”auto_count”,”in_coun”);
}
var_dump($sr_array);
?>
I get this OUTPUT
array (size=9)
0 =>
array (size=1)
'Manoj' =>
array (size=3)
'count_in_battery' => int 10
'count_auto_battety' => int 0
'count_indu_battery' => int 0
1 =>
array (size=1)
'Bharat' =>
array (size=3)
'count_in_battery' => int 10
'count_auto_battety' => int 0
'count_indu_battery' => int 0
2 =>
array (size=1)
'Pradeep =>
array (size=3)
'count_in_battery' => int 10
'count_auto_battety' => int 0
'count_indu_battery' => int 0
If you see the output my array has additional index as
0 => //Unwanted Index
array (size=1)
'Manoj' =>
Which is creating problem in programming. If you can please help me.
Try this:
$sr_array = array();
$sr_sql = "select DISTINCT sr from sales_invoice";
$sr_query = mysqli_query($connection, $sr_sql);
while($sr_result = mysqli_fetch_assoc($sr_query)){
$sr_array[$sr_result["sr"]] = array(
"mc_count" => 0,
"auto_count" => 0,
"in_coun" => 0
);
}

Rearrange an array according to key

I dont know what to ask so i straight away went to show an example. Hope it helps though!
Say i have an array
array (size=3)
0 =>
array (size=3)
0 => int 1
1 => int 2
2 => int 3
2 =>
array (size=3)
0 => int 2
1 => int 3
2 => int 4
5 =>
array (size=3)
0 => int 5
1 => int 6
2 => int 7
Now i want to arrange it according to KEY so that it looks like
array (size=3)
0 =>
array (size=3)
0 => int 1
1 => int 2
2 => int 3
1 =>
array (size=3)
0 => int 2
1 => int 3
2 => int 4
2 =>
array (size=3)
0 => int 5
1 => int 6
2 => int 7
Does anyone has a solution ?
One possible approach:
$new_arr = array_values($old_arr);
You can use foreach and assign the values to new array
$newArr = array();
foreach($array as $k=>$v){
$newArr[] = $v;
}
The most direct way to achieve that is probably to run array_values on your array :
$array = array_values($array);
It will basically reset the keys

Categories