How to set condition to remove from array? - php

I'm trying to unset one record from array but I can't manage it, so I'm asking for someone to help me.
Logic I'm trying to achieve is:
User has an item with possible three categories, he posts a form and than I iterate over array and try to see if category3 matches, if not I go to category2, if not I go to category1. If I find one of the categories that matches array and category asked in form I proceed , but If I don't find any of the categories I unset that record in array.Below you will see an example what I've done now.
I can't get the proper result. Either I unset all the records or unset the record that should stay. I'm trying different conditions for last three days without success. Record I'm talking about is with a key 5.
This is my array:
array (size=5)
0 =>
array (size=9)
'cost_no' => string 'D01' (length=3)
'cost_name' => string 'Carina' (length=6)
'cost_type' => string 'Customs' (length=7)
'cost_measure' => string 'Percent' (length=7)
'cost_amount' => string '5.00' (length=4)
'cost_id' => int 7
'group1' => string '' (length=3)
'group2' => string '' (length=3)
'group3' => string '' (length=3)
1 =>
array (size=9)
'cost_no' => string 'D02' (length=3)
'cost_name' => string 'Banka' (length=5)
'cost_type' => string 'Bank' (length=4)
'cost_measure' => string 'Percent' (length=7)
'cost_amount' => string '0.15' (length=4)
'cost_id' => int 8
'group1' => string '' (length=3)
'group2' => string '' (length=3)
'group3' => string '' (length=3)
2 =>
array (size=9)
'cost_no' => string 'D03' (length=3)
'cost_name' => string 'Ĺ pediter' (length=9)
'cost_type' => string 'Transport' (length=9)
'cost_measure' => string 'Percent' (length=7)
'cost_amount' => string '0.50' (length=4)
'cost_id' => int 9
'group1' => string '' (length=3)
'group2' => string '' (length=3)
'group3' => string '' (length=3)
3 =>
array (size=9)
'cost_no' => string 'D04' (length=3)
'cost_name' => string 'Troškovi firme' (length=15)
'cost_type' => string 'Tax' (length=3)
'cost_measure' => string 'Percent' (length=7)
'cost_amount' => string '21.00' (length=5)
'cost_id' => int 10
'group1' => string '' (length=3)
'group2' => string '' (length=3)
'group3' => string '' (length=3)
5 =>
array (size=9)
'cost_no' => string 'C1' (length=2)
'cost_name' => string 'Cost only for this group' (length=24)
'cost_type' => string 'Warehouse' (length=9)
'cost_measure' => string 'Percent' (length=7)
'cost_amount' => string '12.00' (length=5)
'cost_id' => int 12
'group1' => string 'ACC_P' (length=5)
'group2' => string '1401' (length=4)
'group3' => string '' (length=3)
This is foreach and condition I tried to use to unset if non of the categories match ( it has to check from category3 to category 1).
foreach ($cost_array as $key1 => $value1) {
if (isset($form_group3) AND isset($value1['group3']) AND $value1['group3'] != $form_group3) {
if (isset($form_group2) AND isset($value1['group2']) AND $value1['group2'] != $form_group2) {
if (isset($form_group1) AND isset($value1['group1']) AND $value1['group1'] != $form_group1) {
} else {
unset($cost_array[$key1]);
}
} else {
unset($cost_array[$key1]);
}
} else {
unset($cost_array[$key1]);
}
}
Groups user posts from form:
$form_group1 = mysqli_real_escape_string($conn_mysqli, $_POST['group1']);
$form_group2 = mysqli_real_escape_string($conn_mysqli, $_POST['group2']);
$form_group3 = mysqli_real_escape_string($conn_mysqli, $_POST['group3']);

Related

Find and get array key from multilevel array from other array

i have this array structure, and i want to find and compare with other array that i have.
This is the array that i want search:
array (size=7)
0 =>
array (size=9)
0 => string 'Dorado' (length=6)
1 => string '64GB' (length=4)
2 => string 'Plastico' (length=8)
'vlr' => string '60000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
1 =>
array (size=9)
0 => string 'Blanco' (length=6)
1 => string '32GB' (length=4)
2 => string 'Plastico' (length=8)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
2 =>
array (size=9)
0 => string 'Blanco' (length=6)
1 => string '64GB' (length=4)
2 => string 'Madera' (length=6)
'vlr' => string '60000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
3 =>
array (size=9)
0 => string 'Verde' (length=5)
1 => string '64GB' (length=4)
2 => string 'Madera' (length=6)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
An this is the array with search values:
Array
(
[0] => Blanco
[1] => 32GB
[2] => Plastico
)
I have the key and value, but i need find, in this example the main key is 1 in the long and main array, how can i get that?
PD: the number of search values are the numbers inside main arrays
Let's say that elements is the name of the array that you want to iterate over and find the index whose first three values match the search values. You can simply iterate over the elements, checking if the values match and if they do, then you can store the index in a variable called $wantedKey:
$target = array(
'0' => Blanco
'1' => 32GB
'2' => Plastico
);
$wantedKey = null;
foreach($elements as $key => $elem){
if($elem[0] == $target[0] && $elem[1] == $target[1] && $elem[2] == $target[2]){
$wantedKey = $key;
break;
}
}
echo $wantedKey;
In case you have an arbitrary amount of values, you can use a foreach to iterate over them and check if they match:
foreach($elements as $key => $elem){
$sameValues = true;
foreach($target as $t_key => $t_value){
if($elem[$t_key] != $t_value){
$sameValues = false;
break;
}
}
if($sameValues){
$wantedKey = $key;
break;
}
}
echo $wantedKey;

Remove array 0 in array PHP

I have 2 product id and my code is:
$Final=array();
foreach ($ids as $me)
{
$op=DB::table('product')->where(['id'=>$me])->get();
//$Final[]= $op;
array_push($Final,$op);
}
This code returns:
array (size=1)
0 =>
array (size=1)
0 =>
array (size=15)
'id' => string '34' (length=2)
'title' => string 'گوسفند' (length=12)
'title_url' => string 'sheep' (length=5)
'code' => string 'eerer' (length=5)
'code_url' => string 'eerer' (length=5)
'content' => string '<p>sheep</p>
' (length=14)
'cat' => string '68' (length=2)
'price' => string '50000' (length=5)
'product_state' => string '1' (length=1)
'date' => string '' (length=0)
'order_number' => string '0' (length=1)
'Special' => string '0' (length=1)
'View' => string '0' (length=1)
'number_product' => string '1' (length=1)
'discounts' => string '' (length=0)
I need to remove
array (size=2) 0 => array (size=1) 0 =>
$ids => filter id
for get product number for example (22,34)
I Think you should try this.
$Final=array();
foreach ($ids as $me){
$op=DB::table('product')->where(['id'=>$me])->get();
if($op) {
array_push($Final,$op[0]);
}
}
Then you will get these values.
array (size=2)
0 =>
array (size=15)
'id' => string '34' (length=2)
1 =>
array (size=15)
'id' => string '22' (length=2)
If you are using Any framework then framwork provide us some methods to run query with where in to get all the records in single query.
$op=DB::table('product')->whereIn('id'=>$ids)->get();
you will get array of collection for all the products.

How to get specific data from multidimensional array using php

How to get 'syn' and 'sim' values as a string from the given arrays, as array could vary i only want to extract 'syn' and 'sim' as an example following arrays are given ,i know it might be a simple question but i am new to multidimensional array that's why can't seem to solve it.
array (size=1)
'adjective' =>
array (size=2)
'syn' =>
array (size=7)
0 => string 'cagey' (length=5)
1 => string 'cagy' (length=4)
2 => string 'canny' (length=5)
3 => string 'apt' (length=3)
4 => string 'cunning' (length=7)
5 => string 'ingenious' (length=9)
6 => string 'adroit' (length=6)
'sim' =>
array (size=4)
0 => string 'adroit' (length=6)
1 => string 'artful' (length=6)
2 => string 'intelligent' (length=11)
3 => string 'smar
array (size=2)
'noun' =>
array (size=3)
'syn' =>
array (size=23)
0 => string 'passion' (length=7)
1 => string 'beloved' (length=7)
2 => string 'dear' (length=4)
3 => string 'dearest' (length=7)
4 => string 'honey' (length=5)
5 => string 'sexual love' (length=11)
6 => string 'erotic love' (length=11)
7 => string 'lovemaking' (length=10)
8 => string 'making love' (length=11)
9 => string 'love life' (length=9)
10 => string 'concupiscence' (length=13)
11 => string 'emotion' (length=7)
12 => string 'eros' (length=4)
13 => string 'loved one' (length=9)
14 => string 'lover' (length=5)
15 => string 'object' (length=6)
16 => string 'physical attraction' (length=19)
17 => string 'score' (length=5)
18 => string 'sex' (length=3)
19 => string 'sex activity' (length=12)
20 => string 'sexual activity' (length=15)
21 => string 'sexual desire' (length=13)
22 => string 'sexual practice' (length=15)
'ant' =>
array (size=1)
0 => string 'hate' (length=4)
'usr' =>
array (size=1)
0 => string 'amour' (length=5)
'verb' =>
array (size=2)
'syn' =>
array (size=29)
0 => string 'love' (length=4)
1 => string 'enjoy' (length=5)
2 => string 'roll in the hay' (length=15)
3 => string 'make out' (length=8)
4 => string 'make love' (length=9)
5 => string 'sleep with' (length=10)
6 => string 'get laid' (length=8)
7 => string 'have sex' (length=8)
8 => string 'know' (length=4)
9 => string 'do it' (length=5)
10 => string 'be intimate' (length=11)
11 => string 'have intercourse' (length=16)
12 => string 'have it away' (length=12)
13 => string 'have it off' (length=11)
14 => string 'screw' (length=5)
15 => string 'jazz' (length=4)
16 => string 'eff' (length=3)
17 => string 'hump' (length=4)
18 => string 'lie with' (length=8)
19 => string 'bed' (length=3)
20 => string 'have a go at it' (length=15)
21 => string 'bang' (length=4)
22 => string 'get it on' (length=9)
23 => string 'bonk' (length=4)
24 => string 'copulate' (length=8)
25 => string 'couple' (length=6)
26 => string 'like' (length=4)
27 => string 'mate' (length=4)
28 => string 'pair' (length=4)
'ant' =>
array (size=1)
0 => string 'hate' (length=4)
Any help would be appreciated thanks!
Create a function:
function getArrayAsString($array, $key) {
if (isset($array[$key])) {
return is_array($array[$key]) ? implode(', ', $array[$key]) : $array[$key];
}
return '';
}
Usage example:
$synData = getArrayAsString($array['adjective'], 'sim');
$simData = getArrayAsString($array['noun'], 'syn');

How to combine one array in php

How to my array combine :
$visitor_tracvking_data = json_encode($array);
print_r($visitor_tracvking_data);
Output print_r this:
array (size=6)
0 =>
array (size=2)
'tracktitle' => string 'search_engine' (length=13)
'direct' => string '261' (length=3)
1 =>
array (size=2)
'tracktitle' => string 'search_engine' (length=13)
'social_media' => string '3' (length=1)
2 =>
array (size=2)
'tracktitle' => string 'search_engine' (length=13)
'search' => string '3' (length=1)
3 =>
array (size=2)
'tracktitle' => string 'browser' (length=7)
'chrome' => string '168' (length=3)
4 =>
array (size=2)
'tracktitle' => string 'browser' (length=7)
'firefox' => string '68' (length=2)
5 =>
array (size=2)
'tracktitle' => string 'browser' (length=7)
'netscape' => string '31' (length=2)
How to out put this type:-
array (size=2)
0 =>
array (size=2)
'tracktitle' => string 'search_engine' (length=13)
'direct' => string '261' (length=3)
'social_media' => string '3' (length=1)
'search' => string '3' (length=1)
1 =>
array (size=2)
'tracktitle' => string 'browser' (length=7)
'chrome' => string '168' (length=3)
'firefox' => string '68' (length=2)
'netscape' => string '31' (length=2)
Just loop through the array and generate a new array with tracktitle key -
$new = array();
foreach($array as $key_top => $value) {
foreach($value as $key => $val) {
$new[$value['tracktitle']][$key] = $val;
}
}
return $new;
OR for json encoded string -
return json_encode($new);

How to parse multi-line CSV into multidimensional associative array?

Content of $csv_content is:
1415797658,456ABC,789,123,"bla"
1415797656,654XYZ,897,567,"foo"
1415797654,639HJW,465,146,"bar"
str_getcsv(file_get_contents($csv_content)) results in:
array
0 => string '1415797658' (length=10)
1 => string '456ABC' (length=6)
2 => string '789' (length=3)
3 => string '123' (length=3)
4 => string 'bla' (length=3)
5 => string '1415797656' (length=10)
6 => string '654XYZ' (length=6)
7 => string '897' (length=3)
8 => string '567' (length=3)
9 => string 'foo' (length=3)
10 => string '1415797654' (length=10)
11 => string '639HJW' (length=6)
12 => string '465' (length=3)
13 => string '146' (length=3)
14 => string 'bar' (length=3)
Desired result:
array
0 =>
array
'timestamp' => string '1415797658' (length=10)
'id' => string '456ABC' (length=6)
'id2' => string '789' (length=3)
'id3' => string '123' (length=3)
'text' => string 'bla' (length=3)
1 =>
array
'timestamp' => string '1415797656' (length=10)
'id' => string '654XYZ' (length=6)
'id2' => string '897' (length=3)
'id3' => string '567' (length=3)
'text' => string 'foo' (length=3)
2 =>
array
'timestamp' => string '1415797654' (length=10)
'id' => string '639HJW' (length=6)
'id2' => string '465' (length=3)
'id2' => string '146' (length=3)
'text' => string 'bar' (length=3)
What would be the neatest way to do this?
$array=array();
$handle=fopen($csv_content,'r');
while ($row=fgetcsv($handle)){
$array[]=array(
'timestamp' => $row[0],
'id' => $row[1],
'id2' => $row[2],
'id3' => $row[3],
'text' => $row[4]
);
}

Categories