PHP array_merge result incorrectly on new line - php

I am trying to array_merge id’s from one array into another, it’s merging but putting the new values on a new array line:
IDs to be added: $attachment_ids
array (size=2)
0 => string '2620' (length=4)
1 => string '2621' (length=4)
IDs which already exist and will be added to: $existing_data
array (size=1)
0 => string '2589,2561,2432,2422' (length=19)
result of my array_merge:
$merged_data = array_merge($attachment_ids, $existing_data);
is
array (size=3)
0 => string '2620' (length=4)
1 => string '2621' (length=4)
2 => string '2589,2561,2432,2422' (length=19)
My expected result is:
array (size=1)
0 => string '2620,2621,2589,2561,2432,2422'

If you just need the 1 string with all of the values, you could use implode() with the result you have so far...
$merged_data = [ implode(",", $merged_data) ];

Shortly array merge with array_map
$result=array_map(null,$array1,$array2);

array_merge() requires 2 arrays. The existing data is all in a string. With explode, this data can be converted into an array. With implode() a list is created after array_merge().
$attachment_ids = ['2620','2621'];
$existing_data = ['2589,2561,2432,2422'];
$existing_data = explode(',',$existing_data[0]);
$result = [implode(',',array_merge($attachment_ids,$existing_data))];
//$result: array(1) { [0]=> string(29) "2620,2621,2589,2561,2432,2422" }
Update
Alternatively, strings can be chained manually.
$result = [implode(',',$attachment_ids).','.$existing_data[0]];

Related

how to convert array in php [duplicate]

This question already has answers here:
Convert multidimensional array into single array [duplicate]
(24 answers)
Closed 5 years ago.
Hi i got this array from my database.
array (size=4)
0 =>
array (size=1)
0 =>
array (size=1)
'email_1' => string 'denise#aaa.com' (length=18)
1 =>
array (size=1)
0 =>
array (size=1)
'email_1' => string 'denise#aaa.com' (length=18)
And i need to do get like this
array (size=4)
0 =>
array (size=1)
email_1' => string 'denise#aaa.com' (length=18)
1 =>
array (size=1)
'email_1' => string 'denise#aaa.com' (length=18)
I tried with array_merge and all. But no idea how to archive this?
Do it like below:-
$final_array = array();
foreach($original_array as $key=>$val){
$final_array[$key][] = $val[0]['email_1'];
}
print_r($final_array);
Output:-https://eval.in/848213
In php it is possible to do like this
foreach ($yourArray as $arr){
$result[] = $arr[0];
}
You can get also your desired output like this:
$result = array_map('array_collapse',$yourArray);
For this just assign like this to its first element.
foreach($yourArray as $array){
$array = $array[0];
}

Dynamically creating multidimensional arrays to be encoded into JSON

I have a section of an array defined:
$arrData['categories'] = array();
array_push($arrData['categories'],array("category" => array(array("label"=>"Beef"),array("label"=>"Chicken"))));
array_push($arrDAta['categories'][0]['category'],array("label"=>"pork"));
The arrays are nested so that it encodes into JSON properly with all the {[{[{[]}]}]} formatted properly.
However, I need to create these types of meats dynamically, not statically. When I try to add onto the array in the third line of code to simulate dynamic creation, it throws an error:
Warning: array_push() expects parameter 1 to be an array, null given
Well, $arrData['categories'][0]['category] IS an array, so why does it shoot me down?
To show myself that I'm not crazy, I var_dump $arrData['categories'][0]['category'] and get an array of size 1:
array (size=1)
'category' =>
array (size=2)
0 =>
array (size=1)
'label' => string 'Beef' (length=4)
1 =>
array (size=1)
'label' => string 'Chicken' (length=7)
Its the capitalization. In the third line the variable $arrDAta is capitalized differently than the others ($arrData)

php mysqli array issue

I'm trying to replace this array:
$lts = ['20', '21'];
with an array from the database. I've tried a few things with no avail. Here's what I've got now:
$query = "SELECT id FROM members WHERE admin = '2'";
$lts = mysqli_fetch_all($con->query($query), MYSQLI_NUM);
When I var_dump it its giving me this:
array (size=2)
0 =>
array (size=1)
0 => string '20' (length=2)
1 =>
array (size=1)
0 => string '21' (length=2)
I need it to give me this:
array (size=2)
0 => string '20' (length=2)
1 => string '21' (length=2)
Any idea on how to get the latter array?
I just notice your returning array is same as the array you got,
never mind run this code
//run loop on your array after your query
for($i=0; $i<count($lts); $i++){
for($j=0; $j<count($lts[$i]); $j++){
$lts[$i]=$lts[$i][$j];
}
}
echo '<pre>';
print_r($lts);

preg_match_all php inserting results

the below code searches a very long queried result, "body" and pulls out quoted numbers. I want to insert those numbers into a single column reference table. I had this working when it was a preg_match, but preg_match all has thrown a wrench in and it continues to bark about array-string conversion. Each "body" result can have multiple number results, and I believe this causing multiple arrays. Can anyone help me get this inserted?
$textToReplace = mysqli_query($conn, "SELECT body from T1");
while($row2 = $textToReplace->fetch_array()){
$body = $row2["body"];
preg_match_all('#"\d+"#', $body, $matches);
foreach($matches as $match) {
$id=$match;
var_dump($id);
// $sql = mysqli_query($conn, "INSERT into T2 VALUES($id)");
}
}
This is a snippet of the result of a var_dump on $id:
array (size=1)
0 => string '"10064905"' (length=10)
array (size=3)
0 => string '"10064788"' (length=10)
1 => string '"10064797"' (length=10)
2 => string '"10064807"' (length=10)
array (size=1)
0 => string '"10063833"' (length=10)
array (size=1)
0 => string '"10063824"' (length=10)
array (size=2)
0 => string '"10063805"' (length=10)
1 => string '"10063796"' (length=10)
preg_match_all() returns a 2-dimensional array. By default, $matches[0] contains all the full-regexp matches, the remaining $matches[n] contain the matches for capture group n. In your program, you need to iterate over $matches[0]:
foreach($matches[0] as $match) {
$id=$match;
var_dump($id);
$sql = mysqli_query($conn, "INSERT into T2 VALUES($id)");
}

Count elements in simple array and order them by highest first

I have an array like the example below (from var_dump($tagged);:
array (size=1)
0 =>
array (size=7)
0 => string '#raining' (length=8)
1 => string '#raining' (length=8)
2 => string '#driving' (length=8)
3 => string '#hungrySoon' (length=11)
4 => string '#strangeworld' (length=13)
5 => string '#fruitweekFunky' (length=15)
6 => string '#kevins_rainbow_disco' (length=21)
7 => string '#raining' (length=8)
8 => string '#fruitweekFunky' (length=15)
I am simply after displaying the array as follows (From the most frequent first):
#raining
#fruitweekFunky
#driving ...etc
To achieve exactly what you've asked for (a descending ordered array of the highest occurences to the lowest) step by step:
Count the number of occurences:
$occurences = array_count_values($tagged[0]);
Sort the array (by value, because the number of occurrences is the current array value and the tag is the key - arsort() maintains the original keys):
arsort($occurences);
Get array of the keys for output (because the tags are currently keys):
var_dump(array_keys($occurences));
Fact is, $tagged is an array of arrays. So you need to take its first element.
Try :
$result = array_count_values(array_values($tagged[0]));
var_dump($result);

Categories