How to compare two character in php? - php

Any one explain me how can I compare two characters in php
Here my Code:
$unsorted = Array(
"0" =>"0000C11",
"1" =>"0000A11",
"2" =>"0000C13",
"3" =>"0000D11",
);
$sortArr = array('A','B','C','D');
foreach ($unsorted as $key => $value) {
$val = substr($value,-3,1);
foreach ($sortArr as $key1 => $value1) {
if ($val === $value1 ) {
$sortArrFin[] = $value;
}
}
}
echo "<pre>";
print_r($sortArrFin);
Here I want to check condition if ($val === $value1 ) but it gives always true..
Means if $val = C and $value1 = A ti's return true...
Please help me.
Thanks

Please try following code, actually you have to make inner foreach to outer and outer for loop to inner.
<?php
$unsorted = Array(
"0" =>"0000C11",
"1" =>"0000E11",
"2" =>"0000C13",
"3" =>"0000D11",
"4" =>"0000A11"
);
$sortArr = array('A','B','C','D','E');
foreach ($sortArr as $key => $value) {
foreach ($unsorted as $key1 => $value1) {
$val = substr($value1,-3,1);
if ($val === $value ) {
$sortArrFin[] = $value1;
}
}
}
?>

Related

How to get nth element of a multidimensional array in PHP?

I want to select the "RSI" from the first element only.
Array (json file):
{
"Technical": {
"2019-01-11 15:30": {
"RSI": "123"
},
"2019-01-11 14:30": {
"RSI": "456"
}
"2019-01-11 14:30": {
"RSI": "789"
}
}
My php:
foreach ($json['Technical'] as $field => $value) {
echo $value['RSI']; // Gives 123456789
}
I want only 123
I tried:
echo $value[0]['RSI']; // Gives NULL
Break the loop with break; and it will only return the first item.
foreach ($json['Technical'] as $field => $value) {
echo $value['RSI']; // Gives 123
break;
}
If you want specific items then use a "$key" variable.
$key = 0;
foreach ($json['Technical'] as $field => $value) {
if($key == 0 || $key ==1){
echo $value['RSI'];
}
$key++;
}
// 123456
Change the if to suit your needs.

PHP read value form multidimensional array

its easy for sure..
i have code like this:
$indeks = 0;
foreach ($list as $k => $v)
{
$data['fname'] = $customer->firstname;
$data['lname'] = $customer->lastname;
$data['code'] = $code['code'];
$tablica[$indeks] = $data;
$indeks++;
and i want to read only 'code' value for each array.
i try:
foreach($tablica as $k => $v){
foreach ($v as $key => $value ) {
echo $value
}
}
but i get all arrays values.
when i try
foreach($tablica as $k => $v){
foreach ($v['code'] as $key => $value ) {
echo $value
}
}
i have nothing...
thx for help
You can use array_column function to get all values of column, for example:
foreach (array_column($tablica, 'code') as $value) {
echo $value;
}
I think a For loop should help
for($i=0;$i<count($tablica);$i++){
echo $tablica[$i]['code'];
}
or get all Codes into an Array
$code = array();
for($i=0;$i<count($tablica);$i++){
$code[$i] = $tablica[$i]['code'];
}
You don't need nested loops.
foreach ($tablica as $value) {
echo $value['code'];
}
DEMO

php associative array: no array build with key from variable name

this code works as expected
$fruits = array("d" => "Zitrone", "b" => "Banane", "c" => "Apfel");
$fruits["a"] = "Orange";
//print_r($fruits);
ksort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
ok, now I would like to do the same programmatically:
$collection = array();
foreach ($xml->abschnitte[0]->abschnitt as $sec) {
//echo $sec['strecke']; // this works, the strings are printed out
$collection[$sec['strecke']] = $sec['id'];
}
//print_r($collection); // nothing to see here
//ksort($collection);
foreach ($collection as $key => $val) {
echo $key . " = " . $val . "\n";
}
it seems that there no collection will be build. but there must be a way to build the key up from variables. what do i miss? thanks in advance, mischl

Two different large array

I have problem with two arrays:
$pole1 = array(
array("klic"=>"banan", "jmeno"=>"Banán"),
array("klic"=>"pomeranc", "jmeno"=>"Pomeranč"),
);
$pole2 = array(
array("klic"=>"banan"),
);
Now I need foreach data:
foreach ($pole1 as $key => $val){
//all data from $pole
echo $val
//and here if "klic" from $pole1 == "klic" from $pole2
if ($pole2[$key]["klic"] == $pole1["klic"])
echo "YES"; // - **not working**
}
I need to check if data from $pole1 equals data from $pole2 and write some text but I need to write all data from $pole1.
You meant that?
foreach ($pole1 as $key => $val) {
if ( isset($pole2[$key]["klic"] &&
($pole2[$key]["klic"] == $pole1[$key]["klic"]) )
echo "YES";
}
try this
foreach($pole1 as $k1 => $arrays) {
foreach($arrays as $k2 => $val) {
if($pole2[$k1][$k2] == $val) {
// $pole1[$k1][$k2] is equal to $pole2[$k1][$k2]
}
}
This will echo every entry in pole1 and check every entry in pole1 with every entry in pole2.
foreach($pole1 as $val){
echo($val);
foreach($pole2 as $val2){
if($val['klic']==$val2['klic']) echo 'YES';
}
}

Array in PHP Concatenation

i am trying to generate dynamic array as
foreach($this->data['Carcase'] as $key=> $value)
{
if(!empty($value))
$data[$key]=$value;
}
and got output as
array(
$data['Hieght'] => 5,
$data['Width'] =>6
)
But i need output as
array(
$data['Hieght'] >= => 5,
$data['Width'] >= =>6
)
i tried this
foreach($this->data['Carcase'] as $key=> $value)
{
if(!empty($value))
$data[$key].">=".=$value;
}
this is not Working.Anybody have an Idea About this.
Thanks in Advance.
I'm going to go out on a limb and guess that you want to concatenate the string ">=" to each key if the value is not empty:
$data = array();
foreach ($this->data['Carcase'] as $key => $value) {
if ($value) {
$data[$key . ' >='] = $value;
}
}

Categories