How to merge specific key to one array with PHP? - php

I need to get that value in the array with "clean_bin_number" and pull it out from each of the main arrays. And have only the values in one array.
Hopefully you guys can understand what I'm trying to do....
Here some code examples:
array(2) {
[0]=>
array(1) {
["clean_bin_number"]=>
array(1) {
[0]=>
array(1) {
[0]=>
string(7) "1004445"
}
}
}
[1]=>
array(1) {
["clean_bin_number"]=>
array(1) {
[0]=>
array(1) {
[0]=>
string(7) "3087762"
}
}
}
}
I'd like to get a result like that:
array {
[0] => "1004445",
[1] => "3087762"
}
Thanks

Here are some ways to go about what you want to achieve:
array_walk array_map for foreach
Hint:
foreach($array as $inner)
{
$bin = $inner["clean_bin_number"][0][0];
// ...
}

So your input array is:
$input = array(
array("clean_bin_number" => array(array("1004445")) ),
array("clean_bin_number" => array(array("3087762")) )
);
Now you can very easily just iterate over that array and extract the routes/parts you need, or if those exist not only once, iterate over the sub parts.
Example:
$new_arr = array();
foreach ($input as $element) {
$new_arr[] = $element['clean_bin_number'][0][0];
}
See this working here.
You should have been able to find another solution thou. If not, you have clearly not finished reading/watching beginner tutorials for PHP. Go do that if you want to keep working with PHP. Find Tutorials online like this youtube video or this article.
Asking questions to get specific solutions takes longer than actually doing a bit of learning if you ask me.

Related

find if value exist in php into mysql built array

i build an array from mysql this way
$q="select account_code from chart_master;";
// Generate resultset
$result_set = $con->query($q);
$list = Array();
while( $myrow = mysqli_fetch_array($result_set) ) {
$list[] = $myrow;
}
when i dump $list i get:
array(79) { [0]=> array(2) { [0]=> string(8) "11011001" ["account_code"]=> string(8) "11011001" } [1]=> array(2) { [0]=> string(8) "11011002" ["account_code"]=> string(8) "11011002" } [2]=> array(2) { [0]=> string(8) "11011005" ["account_code"]=> string(8) "11011005" } ...
i now want to check if a value is found in the values 11011001, 11011002 etc with this code:
if (in_array($row['1'], $list))
{
echo $row['1']." found in the array";
}
with $row['1'] being one of the searched value.
I guess I am not looking at the right depth in the array because my in_array does not return anything.
Thoughts?
You should go through each element of your $list array and apply in_array to it.
foreach($list as $listItem){
if(in_array($row['1'], $listItem)){
echo $row['1']." found in the array";
}
}
in_array() only checks one dimension. That's why you need to go iterate through the first dimension and apply it to the second one.
The most succinct version of this I can imagine would be:
$exists = array_search('1001010101', array_column($list, 'account_code')) !== false;
This grabs the account_code column from the multidimensional array and looks inside that column for the value provided. If you only need an existence check, this seems to be a fast way of doing it.
However, if you don't need the rest of that SQL result set, I'd look into maybe doing a COUNT() or using a WHERE to narrow the result set instead. That would be more resource efficient.

multidimensional Array same value remove array

Hello everyone i'm struggling with a issue and couldn't really find my answer on the web.
Maybe i made a mistake than sorry but the answers i found where not exactly where i where searching for.
Here is my problem
["acList"]=>
array(356) {
[0]=>
array(36) {
["Id"]=>
int(434367)
["Rcvr"]=>
int(154)
["HasSig"]=>
bool(true)
["Sig"]=>
int(19)
["Icao"]=>
string(6) "06A0BF"
["Bad"]=>
bool(false)
["Reg"]=>
string(6) "A7-BDA"
["FSeen"]=>
string(21) "/Date(1509481499558)/"
}
[1]=>
array(43) {
["Id"]=>
int(3753696)
["Rcvr"]=>
int(149)
["HasSig"]=>
bool(true)
["Sig"]=>
int(23)
["Icao"]=>
string(6) "3946E0"
["Bad"]=>
bool(false)
["Reg"]=>
string(6) "A7-BDA"
["FSeen"]=>
string(21) "/Date(1509481476453)/"
}
I want that when "Reg" is the same that he removes only one of the same arrays (if possible based on FSeen).
I tried to make new array and combine them and i tried array_unique but that is not doing what i want sadly.
I hope someone can help me out with this.
Its not going to be the most efficient way, or great for large data sets, but this could work:
$aArray = array(array('id' => 1), array('id' => 2), array('id' => 1));
// create a tmp array to hold values we want to check
$aTmpArray = array();
// loop over the array
foreach ($aArray as $iPos => $aItem) {
if(!isset($aTmpArray[$aItem['id']])){
// if the item doesnt exist in tmp array, add it
$aTmpArray[$aItem['id']] = null;
}else{
// if the item exists, remove this entry from aArray
unset($aArray[$iPos]);
}
}
// set for gc
$aTmpArray = null;
It will preserve the first result and remove any subqiquent duplicates/occurances of id.

foreach results in endless loop

I have an array stored in $_SESSION:
var_dump($_SESSION['session_article']);
//result:
array(2) {
[0]=> array(2) {
["id"]=> string(1) "3"
["amount"]=> int(2)
}
[1]=> array(2) {
["id"]=> string(2) "13"
["amount"]=> int(1)
}
}
If I do:
for($artKey = 0;$artKey < count($_SESSION['session_article']);$artKey++){
$cartArt = $_SESSION['session_article'][$artKey];
//stuff that doesn't affect key or value
}
everything is fine ...but if I do:
foreach($_SESSION['session_article'] as $artKey => $cartArt){
//stuff that doesn't affect key or value
}
the page won't stop to load (infinite loading, like the foreach never terminates)
I would like to you know that the computer is not a dumb machine, so whenever you do not get the right result then its not the computers problem, it is in how you code.
So lets take a look at your code first you var_dump($_SESSION['session_article']) you got an array with two elements each being an associative array. Now observe that this is an array of arrays so you code in scenario 2 is wrong.
If it were to be just a single array say $myArray = ['Simon', 'Peter', 'You'] then this woul have worked fine (note I used the short array syntax here you can use array() if you prefer). But the problem is you have multidimensional Arrays so you could should be
foreach($_SESSION['session_article'], list($a,))
{
//stuffs here
}
or better walk through the $_SESSION['session_article'] as an associative array like so
$myArray = $_SESSION['session_article']
$field = count($myArray, COUNT_RECURSIVE - (int)2)
for ($record = 0; $record < $myArray.length; $record++) {
//record number
for ($field = 0; $field < $field ; $field++) {
//combine record and field here
}
}
Hope I could lend a helping hand?
Please checkout
http://php.net/manual/en/function.count.php
http://php.net/manual/en/control-structures.foreach.php
They really have a good doc, just take you time.

Dealing with php array whose content may or may not have another array

I am dealing with arrays whose structure is different depending on the number or items in the array.
For example, the following is the array with one item in it.
// Case #1
["Assignment"]=>
object(stdClass)#29 (9) {
["Id"]=> string(10) "1234567890"
..
}
However if there are more than 1 item in the array,
// Case #2
["Assignment"]=>
array(2) {
[0]=>
object(stdClass)#28 (9) {
["Id"]=> string(10) "1234567890"
..
}
[1]=>
object(stdClass)#28 (9) {
["Id"]=> string(10) "1234567890"
..
}
}
Notice that the contents are in another array for this. No matter of how many items there are, I want to access the Id. $array->Id will work for one case but won't work for the other with the error saying, Trying to get property of non-object.
I could come up with an inefficient way by counting the # of contents in the array like this:
// say the arrays above are declared as $assignment
if($numOfAssignment > 1) {
foreach($assignment as $key) {
echo $key->Id;
}
}
else {
echo $assignment->Id;
}
But if the code was a bit lengthy, I feel it is too repetitive and inefficient.
Is there a way to do this in one effective phrase no matter of the number of the contents inside the array? Let me know if anything is unclear. Thanks!
What you can do is change the non-array into an array with a single element, then you can process it consistently.
if (!is_array($assignment)) {
$assignment = array($assignment);
}
foreach ($assignment as $key) {
echo $key=>Id;
}

advanced unset the value form array PHP

I have two arrays, what consist arrays. I need to merge these arrays recursive. But I need to do this action few times, and
array_merge_recursive()
will apend my data twice, I want to remove element what already exist in target array.
$messages array :
array(2) {
["messages"]=>
array(2) {
["test.testik"]=>
string(13) "Це тест"
["test2313.tes31231tik"]=>
string(23) "це тестончик"
}
["validators"]=>
array(4) {
["valid.validik"]=>
string(36) "Це валідне значення"
["joga.jimbo"]=>
string(27) "Джімбо торбінс"
["validka.invalidka"]=>
string(23) "це інвалідка"
["smith.john"]=>
string(17) "джон сміт"
}
}
$allCar array:
array(2) {
["messages"]=>
array(1) {
["test2313.tes31231tik"]=>
string(23) "це тестончик"
}
["validators"]=>
array(2) {
["validka.invalidka"]=>
string(23) "це інвалідка"
["smith.john"]=>
string(17) "джон сміт"
}
}
I wrote some code:
foreach ($messages as $domain => $messagesArray) {
foreach ($allCat as $d => $mess) {
if ($domain == $d) {
foreach ($messagesArray as $ymlkey => $trans) {
foreach ($mess as $ymlk => $transl) {
if ($ymlkey == $ymlk) {
unset($mess[$ymlk]);
}
}
}
}
}
}
Then when I run recursive merge it append the same values to array. What I am doing wrong ?
This:
foreach ($allCat as $d => $mess) {
$mess is a temporary COPY of whatever value your foreach() loop is currently working on. When you do your unset($mess...) later on, you're simply unsetting that temporary copy.
While some might suggest making $mess a reference, this can/will cause problems later on, because $mess will STILL be a reference after the loops end, and reusing the variable later on will now be mucking around with whatever $mess last pointed at in the loop.
Instead, use the full array/object path reference in the unset call:
unset($messages[$domain][$d][$ymlkey][$ymkl])
or whatever it should be. This way you guarantee you're working with the actual "real" array, and not any of the many temporary copies your nested loops are creating.

Categories