Jump to content

Split an array element and place it back in array


tHud

Recommended Posts

Hello everyone :)

 

I have an array like this...

Array
(
    [0] => A27
    [1] => A25+A26
    [2] => B1
    [3] => B7
    [4] => C10
    [5] => A3
    [6] => B5
    [7] => C1+C2
    [8] => A1
    [9] => A6
    [10] => C8
    [11] => A12
    [12] => C14
    [13] => A15
    [14] => C11
    [15] => B3+B4
)

I would like to remove the values after the + sign and replace the second part back in the array.

 

Something like this...

Array
(
    [0] => A27
    [1] => A25
    [2] => B1
    [3] => B7
    [4] => C10
    [5] => A3
    [6] => B5
    [7] => C1
    [8] => A1
    [9] => A6
    [10] => C8
    [11] => A12
    [12] => C14
    [13] => A15
    [14] => C11
    [15] => B3
    [16] => A26
    [17] => C2
    [18] => B4
)

I think I've been away from PHP too long because I've tried all sorts of ways of doing this and I am really stuck. :(

 

Thank you for any suggestions. :)

Link to comment
Share on other sites

I'm sorry, this is the code.

$query = "SELECT assignTable FROM EXHIBITORS WHERE year = $newYear";

$result = $mysqli->query($query) or die("There was a problem with the query: " . mysqli_error($mysqli));

if ($result->num_rows > 0) {
		while($row = $result->fetch_assoc()) {$myResults[] = $row; }
}

$myVals = array_column($myResults, 'assignTable');

echo '<pre>';
print_r($myVals);
echo '</pre>';

I've been asked to alter some code of a guy that left the company.
 

Link to comment
Share on other sites

So the problem exists because of a poor database design. Combining values in a single column is not the way to store data.

 

Anyway try this:

if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
// break up the values at the + sign
$myResults = explode('+', $row['assignTable']);
foreach($myResults as $d)
$myVals[] = trim($d);
}
}

Edited by ginerjm
Link to comment
Share on other sites

Your code is vulnerable to an SQL Injection Attack. You never ever put variables in a query. You need to use prepared statements. It is also a security problem outputting DB errors to the user. That info is only good to a programmer or a hacker. I suspect you have many other problems as well.

Edited by benanamen
Link to comment
Share on other sites

So the problem exists because of a poor database design. Combining values in a single column is not the way to store data.

 

Anyway try this:

 

Oh thank you so much ginerjm, I'm always overthinking things!

 

I really appreciate your help. :)

 

 

 

@benanamen - Thank you for your input. I am aware of this, it's not my code. I only saw it today for the first time.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.